anomalous behaviours of jQuery ajax function's success
I wrote following code :
$.ajax({ url: link + "?" + Math.random(), success: function (response) {
alert(response);
}});
Although the alert gives me what responseText
should give ideally when I use from glassfish. But when I loaded exactly same file in VS, to my horror,开发者_如何学C I got [Object]
as the output of alert.
What's wrong?
By the way, what I return is XML not JSON.
jquery will by default perform an "intelligent guess" on your data type and pass the success function the formatted response. So if, for example, your url supplies json data the success function will be handed the parsed json object, not just a string. So alert({...})
will show [object Object]
If you want just the textual output, use:
$.ajax({
url: link + "?" + Math.random(),
success: function (response) { alert(response); },
dataType: 'text'
});
I would suspect that in the second case JSON is being returned you will need to do:
$.ajax({ url: link + "?" + Math.random(), success: function (response) {
$(response).each(function() {
/*do something with data, firefox with firebug allows you to do console.log($this) which will show you the data in a window below the browser, Chrome also has a similar feature, alerting in an iterator is never a good idea.*/
})
}});
精彩评论