1

I want to download a wiki-format preset into a textarea with jquery:

<form><textarea  name="wikitext" id="submittextarea"></textarea></form>

I use this script:

function download_to_textbox(url, el) {
        $.get(url, null, function (data) {
            // this is not reached, why???
            console.log("test")
            el.val(data);
        }, "text");
}
url='http://freifunk.in-kiel.de/mediawiki/api.php?action=parse&prop=wikitext&page=Template:Node';
download_to_textbox(url, $('textarea[name="wikitext"]'));

there is nothing in the textarea, what am I doing wrong?

rubo77
  • 19,527
  • 31
  • 134
  • 226
  • is the data getting returned - have you logged the data the ajax call returns to the console? – hammus Nov 21 '13 at 23:50
  • 3
    Is that a cross-domain request? Have you verified that the URL is being hit? – Cᴏʀʏ Nov 21 '13 at 23:50
  • 5
    It is a same origin policy violation – Arun P Johny Nov 21 '13 at 23:50
  • firebug sais, the download via GET is "200 OK" – rubo77 Nov 21 '13 at 23:52
  • What does Firebug say the response *body* is? – Cᴏʀʏ Nov 21 '13 at 23:53
  • Can you try a console.log(data) inside the function and confirm the correct response appears in chrome developer tools (console tab) or Firebug (again console tab). – steve Nov 21 '13 at 23:57
  • I did and it doesent log "test". console logging outside of `$.get(url...` works – rubo77 Nov 22 '13 at 00:00
  • 2
    Cross-domain does not work. You can get it to work using JSONP in which you need a matching callback You can get it to work by setting up a Proxy like mentioned already Javascript only - not so sure if you will find a solution. Not likely – WebsterDevelopine Nov 22 '13 at 00:16
  • you are right, It was because of cross-site-scripting restrictions, I downloaded the site in the lolal folder and changed `url = 'NodeVorlage.xml'` and now it all works fine. If you put that into an answer, I can mark that solved – rubo77 Nov 22 '13 at 08:38

1 Answers1

1

For this I would use a server side script (PHP) as a proxy to get contents to avoid the cross domain issues:

Call this proxy.php

$url='http://freifunk.in-kiel.de/mediawiki/api.php?action=parse&prop=wikitext&page=Template:Node';

$contents = file_get_contents($url);

echo $contents;

Call proxy.php from any AJAX call

You can make the request dynamic so you can reuse the proxy like so using $_POST[url] variable.

$contents = file_get_contents($_POST[url]);
echo $contents;
hammus
  • 2,602
  • 2
  • 19
  • 37
  • I need a javascript only solution in this case – rubo77 Nov 21 '13 at 23:56
  • JSONP would work but it needs a callback function and you need to have that defined on both sides. I think a proxy is your best bet – WebsterDevelopine Nov 21 '13 at 23:57
  • yep my dyslexia sorry – WebsterDevelopine Nov 21 '13 at 23:58
  • I don't have to load it from another domain, so I don't need neither PHP nor JSONP. but thanks ;) – rubo77 Nov 22 '13 at 08:54
  • Did you get this working rubo? I went to look for language encoding because I noticed you had German content but through that I see that you are calling a XML file (with html wrapper) but using dataType: 'text' whereas it should be html or xml depending on how you are unwrapping that xml block http://freifunk.in-kiel.de/mediawiki/api.php?action=parse&prop=wikitext&page=Template:Node – WebsterDevelopine Nov 24 '13 at 02:13
  • One more question Rubo - you are running on the domain http://freifunk.in-kiel.de/ with same subdomain? different subdomain will flag in browser as cross-domain error – WebsterDevelopine Nov 24 '13 at 02:17
  • related: [how-to-set-x-frame-options-on-iframe](http://stackoverflow.com/questions/27358966/how-to-set-x-frame-options-on-iframe) – rubo77 Aug 11 '16 at 02:10