开发者

JSON being returned is structures as function call. Can't figure out how to parse with jQuery

开发者 https://www.devze.com 2023-01-13 13:12 出处:网络
I\'m talking to a s开发者_如何学JAVAerver and the JSON response looks like this: someResponse({ \"Response\":{\"status\":\"3\",\"message\":\"Not valid bla bla\"} });

I'm talking to a s开发者_如何学JAVAerver and the JSON response looks like this:

someResponse({ "Response":{"status":"3","message":"Not valid bla bla"} });

Proper JSON should look like this, right?:

{
    "response":
    {
        "status":"3",
        "message":"Not valid bla bla"
    }
}

Is there a way I could somehow reach that data with jQuery if enclosed in that "someResponse" function?

My jQuery code looks like this:

$.ajax({
   url: "https://someurl/test/request.asp?user=x&pass=x",
   dataType: "JSONP",
   success: function(msg){
    $("#json_here").html(msg.response.status);
   },
   error:function(x,e){alert(x+" :: "+e)}
});

Of course, nothing happens when I do that. But if i do $("#json_here").html(msg); then I ges the full response as above.

Am I doing something wrong or is this an invalid way of sending JSON data? I'm not getting parse errors.

Thanks in advance.


The server returns JSONP, not JSON. This is done to facilitate cross-domain requests (in other words ajax mashups).

All you need to do is implement a function called someResponse to parse the answer and insert the loaded response into your page. Your someResponse will then be called automatically by the browser. You could also have a look at one of the many tutorials on JSONP.

Often times APIs returning JSONP allow the client to choose a name for the function that should be called with the loaded JSON. So you can rename your handler as you want, you just need to tell the server about it.

0

精彩评论

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