开发者

getting raw json string use jquery ajax method meet problem

开发者 https://www.devze.com 2023-01-04 13:02 出处:网络
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 st

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/9787802开发者_JAVA技巧057388?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 dataType to text prevents jQuery handling the request as a JSONP. jQuery does some magic in the background for these type of requests (substituting callback=? in the URL for a function name, and defining the success function 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 dataType to jsonp works, but of course an object is returned.

$.ajax({
    url:"http://api.douban.com/book/subject/isbn/9787802057388?alt=xd&callback=?",
    dataType:'jsonp',
    success:function(data){
        alert(data);
    } //endof success
}); //endof .ajax​​​​

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.

0

精彩评论

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