success-<\", i.e. empty data. Pasting the url in my browser gives me the expected content." />
开发者

Getting data out of the company wiki using jQuery and ajax

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/wik开发者_JS百科i/api.php?action=query&titles=Page%20Title&format=xml&prop=revisions&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?


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.


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&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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜