Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery JSONP cross domain call not doing anything

Tags:

jquery

jsonp

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?callback=?',
  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});
like image 645
David Meagor Avatar asked Dec 21 '22 22:12

David Meagor


1 Answers

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

like image 200
Nick Craver Avatar answered Jan 10 '23 14:01

Nick Craver