开发者

JQuery JSONP cross domain call not doing anything

开发者 https://www.devze.com 2023-01-19 17:20 出处:网络
Whenever I do a JSONP call through jquery to any page I set up (either locally or on my server) all I get is the silent treatment.Firebug reports 200 OK and the response looks ok.I setup alerts boxes

Whenever I do a JSONP call through jquery to any page I set up (either locally or on my server) all I get is the silent treatment. Firebug reports 200 OK and the response looks ok. I setup alerts boxes to popup on success or failure but neither appears. It doesn't seem to matter what url I use, nothing pops up.

BUT if I use the twitter json page then I the success alert box appears as expected so there is obviously something wrong with my response but I don't know what.

As an experiment I copied the twitter json response and uploaded it to my booroo.com domain. It should be identical, but still nothing. I set the headers on the response page to "application/json" and utf-8 but still nothing.

Please help, I've spent all day on this now and I don't know what else to try.

$.ajax({
  dataType: 'jsonp',
//  url: 'http://booroo.com/json.asp?callback=?',
  url: 'http://twitter.com/users/usejquery.json?call开发者_高级运维back=?',
  success: function () {
        alert("Success");     
    },
    error: function(x,y,z) {
        alert("error"+x.responseText);
    }       
});

The response json.asp file contains the following classic ASP headers and then the json response copied from twitter (i've tried others without any success either.)

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Response.Expires = 0
Response.Expiresabsolute = Now() - 1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"
response.ContentType="application/json"
Response.CodePage = 65001 
Response.CharSet = "UTF-8" 
%>({"test_param":12345});


You're having issues because that's not how the response actually looks :)

When you specify jsonp or callback=? it gets replaced, it's actually doing: ?callback=functioName, which turns your response from something like this:

{"test_param":12345}

To this:

functionName({"test_param":12345});

That's needed for JSONP to work. Check out the updated URL to see what I mean: http://twitter.com/users/usejquery.json?callback=functionName


OK after a good nights sleep I've solved the problem. I didn't realise these was a difference between the two response formats. When I queried twitter just using my browser the response didn't include the function name which confused me.

//JSON
{"name":"stackoverflow","id":5}
//JSONP
func({"name":"stackoverflow","id":5});
0

精彩评论

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