最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery JsonP renders error Uncaught ReferenceError: jQuery19107590448246337473_1375193471216 is not defined - Stack

programmeradmin4浏览0评论

I'm not sure what to make of this error, every request has a unique string. The jsonP i'm requesting is a jsonP proxy that, as far as I can tell, is configured as it should.

var url = '/?url=/'+who+'/'+where+'&callback=?';
        $.getJSON(url, function (data) {
            //console.log(data)
        });

response:

jQuery19107590448246337473_1375193471216({"status":{"http_code":200},"contents":"//an html page wrapped in a json-object, I can't post it because it hangs chrome when I try to push the code button i stackoverflow. "})

I Run the code in greasemonkey on a https-page.

I'm not sure what to make of this error, every request has a unique string. The jsonP i'm requesting is a jsonP proxy that, as far as I can tell, is configured as it should.

var url = 'https://phpproxy-dev.herokuapp./?url=http://personer.eniro.se/resultat/'+who+'/'+where+'&callback=?';
        $.getJSON(url, function (data) {
            //console.log(data)
        });

response:

jQuery19107590448246337473_1375193471216({"status":{"http_code":200},"contents":"//an html page wrapped in a json-object, I can't post it because it hangs chrome when I try to push the code button i stackoverflow. "})

I Run the code in greasemonkey on a https-page.

Share Improve this question edited Jul 30, 2013 at 15:02 apsillers 116k18 gold badges247 silver badges247 bronze badges asked Jul 30, 2013 at 14:18 HimmatorsHimmators 15.1k39 gold badges136 silver badges233 bronze badges 15
  • 1 @Spokey It's what jQuery generates for JSONP. As you can see, the OP isn't using that. But they're using $.getJSON and providing the callback=? in the URL – Ian Commented Jul 30, 2013 at 14:19
  • 1 @Neal: The error is in the title – user2437417 Commented Jul 30, 2013 at 14:21
  • 2 Since jQuery will create a function with the correct callback name, I could only imagine that you change the callback name in your server script somehow. – Felix Kling Commented Jul 30, 2013 at 14:24
  • 2 Your code works for me. jsfiddle/GPD7r. (Using jQuery 1.9.1, which seems to be the same as yours). – Matt Commented Jul 30, 2013 at 14:27
  • 1 In that case, you need to figure out exactly what is being considered the global scope in your context. That's where the callback needs to be defined. you probably won't be able to use jQuery to make this ajax request if the global context isn't the window. – Kevin B Commented Jul 30, 2013 at 14:58
 |  Show 10 more ments

3 Answers 3

Reset to default 3

JSONP call in jQuery works the following way:

jQuery defines the callback function in the global scope (with unique name like jQuery19107590448246337473_1375193471216).

Then it adds the script to the document, which is expected to return the code which looks like

jQuery19107590448246337473_1375193471216(<some JSON data here>);

The problem with Greasemonkey is that your code works in the sandbox, and those global scope is in fact the sandbox's global scope, not the actual window scope (unsafeWindow). And this sandbox is normally not accessible from the outside.
As the result, the requested script fails to call this callback function jQuery19107590448246337473_1375193471216.

However the good thing here is that you just don't need JSONP call here at all.
JSONP is developed as a workaround for cross-origin restriction. But GreaseMonkey provides you with nice GM_xmlhttpRequest function, which doesn't have such restrictions.
So you can directly send GET requests from your script to other domains, without using JSONP wrappers.

Try using $.ajax and passing the args you need:

var who = "john";
var where = "london";

$.ajax({
    url:'https://phpproxy-dev.herokuapp./',
    data: {
        'url':'http://personer.eniro.se/resultat/'+who+'/'+where
    },
    dataType: 'jsonp',
    jsonp: 'callback',
    error: function(jqXHR, textStatus, errorThrown){
        console.log(textStatus + errorThrown);
    },
    success: function(data){
        console.log(data); //data.contents has the HTML
    }   
});

See Fiddle here: http://jsfiddle/RaphaelDDL/mZG83/ (I'm outputting the HTML on the textarea in this example).

Apperently, Greasemonkey doesn't support jsonP. I had much more success with GM_xmlhttpRequest that disables the xhr-block.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论