5

I'm trying to get a jsonp callback working using jquery within a greasemonkey script. Here's my jquery:

$.ajax({
    url: "http://mydomain.com/MyWebService?callback=?",
    data: { authkey: "temphash" },
    type: "get",
    dataType: "json",
    cache: false,
    success: function(data) {
        console.log(data);
    }
});

in my webservice (asp.net) I'm returning the response with a content type of application/javascript. The response the server is actually sending back is:

jsonp1276109314602({"message":"I'm getting tired of this not working"})

The jsonp1276109314602 method name is being randomly generated by jquery, and I'm grabbing it with Request.QueryString["callback"]

However my success function is never called and the firebug console gives me an error saying jsonp1276109314602 is not defined.

What am I doing wrong?

NOTE I'm making this call from a greasemonkey script on a craigslist page. It's a cross-domain request, but I can see the request is actually making it to the server and returning a good response, but for whatever reason the registered callback that jquery creates appears to not exist when the response comes back. It works fine if I run the script in the firebug console from the craigslist page, but not when it's run from the greasemonkey script.

Micah
  • 111,873
  • 86
  • 233
  • 325
  • Are you embedding jQuery in the GreaseMonkey script or loading it separately? – James Jun 09 '10 at 19:18
  • ... Or, is jQuery already available on the page that you're enhancing? – James Jun 09 '10 at 19:19
  • I've got it loaded in via the greasemonkey script – Micah Jun 09 '10 at 19:28
  • 1
    you can solve this problem using jsonp.Please follow the link below. This site also contains demo code. [http://www.onlinesolutionsdevelopment.com/blog/web-development/javascript/jsonp-example/](http://www.onlinesolutionsdevelopment.com/blog/web-development/javascript/jsonp-example/) Hope this can solve your problem. –  May 21 '12 at 10:17

3 Answers3

3

Have you already tried:

$.ajax({
    url: "http://mydomain.com/MyWebService",
    data: { authkey: "temphash" },
    type: "get",
    dataType: "jsonp",
    cache: false,
    success: function(data) {
        console.log(data);
    }
});

From docs:

"jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback.

I haven't examined jQuery's source code but it's possible that the callback function isn't created unless you specify jsonp for the dataType option.

Ken Browning
  • 28,693
  • 6
  • 56
  • 68
2

It turns out that you have to do some extra monkeying around (pun intended) to get it to work inside a greasemonkey script.

The long answer can be found here: jQuery.getJSON inside a greasemonkey user script.

The short answer is to ditch the JSONP approach and include this in your script:

// @require http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js

Not sure I understand it all, but nonetheless it works like a champ and I'm able to make cross domain requests from my script.

Community
  • 1
  • 1
Micah
  • 111,873
  • 86
  • 233
  • 325
  • 1
    If you're interested in how the `gm_jq_xhr.js` script works, you can read [an explanation](http://ryangreenberg.com/archives/2010/03/greasemonkey_jquery.php) I wrote. – Ryan Aug 29 '10 at 02:19
1

I think you're supposed to use 'jsonpCallbackString' instead of 'success'.

http://api.jquery.com/jQuery.ajax/

tkane2000
  • 11
  • 1