3

We have been using JSONP (http://code.google.com/p/jquery-jsonp/) to do some localhost calls to retrieve JSON objects... I have upgraded to Firefox 4 today and now the code we were using doesn't work in Firefox 4, but it still works in IE, Chrome and Safari.

With the JSONP plugin it appends a script tag with a load of stuff in it... effectively it is like XSS... So I wondered if Firefox were trying to prevent this now.

Code:

    $.jsonp({
        url: "http://localhost:2020/wsService/LocalResources/All",
        callback: "callback",
        success: function(data) {
            // some success code
        },
        complete: function(xOptions, textStatus) {
            // this code doesn't alert in firefox 4
            alert("Complete");
        },
        error: function(xOptions, textStatus) {
            // error code
        }
    });

And then it appends this string

<script id="_jqjsp1" async="" src="http://localhost:2020/wsService/LocalResources/All?_1300967068015=">

Failed to load source for: http://localhost:2020/wsService/LocalResources/All?_1300967068015=

Joshua Smickus
  • 1,011
  • 10
  • 14
  • What errors do you see in the console if you run your code under Firebug, say? I think it's probably more likely that your particular code is showing some odd interaction with Firefox 4... – Matt Gibson Mar 23 '11 at 16:20
  • @Josh Well, my blog uses a tiny bit of JSONP (it grabs a country code from a JSONP geolocator service to figure out whether to serve US or UK Amazon ads) and that's working fine in Firefox 4.0; I just tested it. Can you reduce your code down to a small example that's not working and post it? – Matt Gibson Mar 23 '11 at 16:46
  • well it is like this: $.jsonp({ url: "http://localhost:2020/wsService/LocalResources/All", callback: "callback", success: function(data) { // do success stuff }, error: function(xOptions, textStatus) { // do error stuff } }); – Joshua Smickus Mar 23 '11 at 17:26
  • 1
    @Josh I've edited your code into your question. Do you really have that semicolon after the URL string, or is that a typo? – Matt Gibson Mar 23 '11 at 20:59
  • think that was a typo - just checked the code in the js file and it isn't there... oops. have updated your code edit... – Joshua Smickus Mar 24 '11 at 09:22
  • Okay -- incidentally, it looks like there may be something specific going on with **localhost** callbacks -- see http://stackoverflow.com/questions/1217926/jsonp-callback-doesnt-execute-when-running-at-localhost -- perhaps you could try giving your site a real name, if only locally, and seeing what happens? – Matt Gibson Mar 24 '11 at 09:23
  • tried using http://127.0.0.1 and tried http://localhost. ... do you mean changing my hosts file so http://mysite points to http://127.0.0.1 instead? – Joshua Smickus Mar 24 '11 at 09:30
  • @Josh Yeah, something like that. For example, I often use "www.mysite.com.test", or whatever, as a local test server name. But the first thing I'd try is what was suggested as a quick hack in that other question, which is to use "`localhost.`" rather than "`localhost`", i.e. add the DNS "root" dot to the end. That seems to be fooling whatever's preventing the calls from working, so it's probably worth a quick try. – Matt Gibson Mar 24 '11 at 09:35
  • I have tried "localhost.", "127.0.0.1" and have also edited my hosts file and it still doesn't work in Firefox 4. :/ – Joshua Smickus Mar 24 '11 at 09:48
  • Also, I have monitored traffic using Fiddler and can see that no requests are made to the URL in Firefox, but they are made in IE/Safari/etc.. – Joshua Smickus Mar 24 '11 at 09:59
  • @Josh and you're definitely getting no errors at all in the debugging console? That's quite weird; if it were a security restriction I'd expect you to be seeing errors. Previous versions of Firefox were very clear in their console about what they were preventing. – Matt Gibson Mar 24 '11 at 10:02
  • It appends the script tag: but then when I expand it in firebug it says: Failed to load source for: http://localhost.:2020/wsService/LocalResources/All?_1300965865087= – Joshua Smickus Mar 24 '11 at 11:25
  • and I have tried hosting the json file locally and remotely and it will not load the source for any of them – Joshua Smickus Mar 24 '11 at 11:28
  • @josh Can you edit that back into your question? Thanks. Also, what happens when you just go to that specified URL in your browser? Do you see your JSONP content? (And what Content-type is your server returning, just in case that's relevant...) – Matt Gibson Mar 24 '11 at 11:31
  • The original localhost:2020 address returns it as application/json. so I saved this file, then copied it locally to htdocs root, and copied it to the remote server root. – Joshua Smickus Mar 24 '11 at 11:50
  • Oh and I see the JSONP content yeh, all looks fine to me... If the returned content was bad surely it would error. – Joshua Smickus Mar 24 '11 at 11:59
  • Well, I'm pretty close to "completely out of ideas" here, I'm afraid. Erm. Humour me for a second, will you, and try serving the JSONP as `application/javascript` instead of `application/json`? – Matt Gibson Mar 24 '11 at 12:42
  • Tried that and it still doesn't work... :/ I asked a question on the Firefox site see if they can clear anything up... – Joshua Smickus Mar 24 '11 at 14:54

2 Answers2

0

Use "window.alert" inside the function instead of "alert"... this did the trick in my case... Hope this helps...

0

I am also using jsonp in one of my projects and I tested it a few weeks back and it was not working. However I tested it again today and it seems to be working fine:

I tested this on Firefox 4.0, 4.0.1 on Windows and Linux with jQuery 3.1.2, 4.1.2 using jsonp: 2.1.2

Here is the code I used:

jQuery.jsonp({
   url: "http://api.twitter.com/1/statuses/user_timeline.json?include_rts=t&screen_name=twitter&rpp=20&callback=?",
   success: function(data) {
      alert("Success");
   },
   complete: function(xOptions, textStatus) {
       alert("complete");
   },
   error: function(xOptions, textStatus) {
       alert("Error");
   }
});

The only difference is that I am passing the callback at the end of the url as "callback=?" instead of defining it in the request options (not really sure if it matters).

U.Ahmad
  • 618
  • 1
  • 6
  • 8