I use jquery ajax method, set datatype json, I get a jsonp response from a cross-domain server. But what i want is raw string of json response. so i set datatype text, but i got nothing but a empty string.
$.ajax({
url:"http://api.douban.com/book/subject/isbn/9787802057388?alt=xd&callback=?",
dataType:'text',
success:function(data){
alert(data);
} //endof success
}); //endof .ajax
can any one tell me why?
if use getJSON method to do this, how can i get a raw string of json?
Setting the
dataTypetotextprevents jQuery handling the request as a JSONP. jQuery does some magic in the background for these type of requests (substitutingcallback=?in the URL for a function name, and defining thesuccessfunction as a global function).Why do you want the response to be raw text? It isn’t possible to get a response that is just JSON from a JSONP request, because the nature of JSONP requires the response is wrapped in a function call.
Setting the
dataTypetojsonpworks, but of course an object is returned.If you want a string, you could double-json-encode a part of the response on the server, so that it is received as a string, or use a JavaScript JSON encoder on the client and encode it again, but both don’t really seem ideal solutions. An object is much more usable and useful.