views:

34

answers:

2

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

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

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

Why am I not getting any data?

A: 

If data is an object you will receive the close results. Try use typeof data in the alert.

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

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

I just tried execute the following code

try {
    $.ajax({url:"http://en.wikipedia.org/w/api.php?action=query&amp;titles=jQuery&amp;format=xml&amp;prop=revisions&amp;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 try to use a crossdomain call. In IE I can see "exception" alert. In Chrome and Firefox: "not ok". The function success will NOT be called in case of error.

So the data from server are really an empty string ("") for the url of Tobbe.

To Tobbe: you should add the last parameter "xml" probably.

Oleg
'typeof data' returns "string"
Tobbe
are you sure its no crossdomain call?
jAndy
A: 

You might breach the ajax cross domain policy there. Is that domain you try to access yours? better said, the one from your script?

From my experience, if you try to access data from a foreign domain, the success handler will fire regardless. But as you describe, with no data at all.

jAndy
In case of error the success callback will be not called, so $.get was successful and returns something
Oleg
read again carefully, if you generate a jQuery ajax call to a foreign domain, the success handler is called with empty data (tested in Chrome)
jAndy
Looks like you are right. Tried with GM_xmlhttpRequest using the same url. That works. (GreaseMonkey allows the requests to cross the "same origin policy" boundaries)
Tobbe