success- <", which is empty data. Pasting the url in m...">

Getting data from a wiki using jQuery and ajax

The following javascript code gives me "> success- <", which is empty data. Pasting the url in my browser gives me the expected content.

$.get("http://company.tld/wiki/api.php?action=query&titles=Page%20Title&format=xml&prop=revisions&rvprop=content", function (data, status) {
    alert(">" + status + "-" + data + "<");
});

      

This is the MediaWiki wiki. Here's the MediaWiki API specification: http://www.mediawiki.org/wiki/API:Query

Why am I not receiving any data?

+2


a source to share


2 answers


You can violate ajax cross domain policy. Are you trying to get this domain to yourself? better said, one of your script?



From my experience, if you try to access data from a foreign domain, the success handler will fire regardless. But, as you described, without any data.

0


a source


If data

is an object, you will get the closing results. Try using typeof data

in an alert.

UPDATED : To jAndy: In the jQuery.ajax documentation, we can read the following (see http://docs.jquery.com/Ajax_Events ):

  • success (local event). This event is fired only if the request was successful (no errors from the server, no errors with data).

I just tried following code



try {
    $.ajax({url:"http://en.wikipedia.org/w/api.php?action=query&titles=jQuery&format=xml&prop=revisions&rvprop=content",
            success: function (data, status, x) {
             alert ("ok");
            },
            error: function (data, status, x) {
             alert ("not ok");
            },
            dataType:"xml"});
} catch (e) {
    alert ("exception");
};

      

where I am trying to use the crossdomain call. In IE I see an "exception" warning. Chrome and Firefox: "not ok". The success of the function will NOT be called on error.

So the data from the server is indeed the empty string ("") for the Tobbe url.

To Tobbe : you must add the last "xml" parameter.

0


a source







All Articles