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 = 'https://phpproxy-dev.herokuapp.com/?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.

apsillers
  • 112,806
  • 17
  • 235
  • 239
Himmators
  • 14,278
  • 36
  • 132
  • 223
  • 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 Jul 30 '13 at 14:19
  • Could you show how you are requesting this endpoint in your javascript code? – Darin Dimitrov Jul 30 '13 at 14:20
  • @neal error Uncaught ReferenceError: jQuery19107590448246337473_1375193471216 is not defined – Himmators Jul 30 '13 at 14:21
  • 1
    @Neal: The error is in the title –  Jul 30 '13 at 14:21
  • @Neal I think this code is at the end of some block, that's why I indented the `var url = ...` line. The `}` at the end of the code made me think that – Ian Jul 30 '13 at 14:22
  • @DarinDimitrov what does that mean? – Himmators Jul 30 '13 at 14:23
  • 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 Jul 30 '13 at 14:24
  • http://stackoverflow.com/questions/16148723/backbone-js-uncaught-referenceerror –  Jul 30 '13 at 14:25
  • 2
    Your code works for me. http://jsfiddle.net/GPD7r/. (Using jQuery 1.9.1, which seems to be the same as yours). – Matt Jul 30 '13 at 14:27
  • @Matt That's weird, I run it using gresemonkey,(tampermonkey, for chrome), that might have something to do with it – Himmators Jul 30 '13 at 14:31
  • If you are using greasemonkey why don't you just request the page directly – Esailija Jul 30 '13 at 14:34
  • @Esailija It's part of a script that makes multiple requsts, also, greasmonkey apparently doesn't run across multiple websites – Himmators Jul 30 '13 at 14:37
  • Do you really need jQuery for this? the jsonp request itself is easy enough to do without jQuery: http://jsfiddle.net/n3p9B/ – Kevin B Jul 30 '13 at 14:38
  • @KevinB Well, no But I get the same error nevertheless: Uncaught ReferenceError: myjsonpcallback is not defined ?url=http://personer.eniro.se/resultat/%20Khalidah%20Medhat%2019690605-1229/%20281%2040%20H%C3%84SSLEHOLM&callback=myjsonpcallback:1 (anonymous function) – Himmators Jul 30 '13 at 14:57
  • 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 Jul 30 '13 at 14:58

3 Answers3

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.

amakhrov
  • 3,820
  • 1
  • 13
  • 12
2

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

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

$.ajax({
    url:'https://phpproxy-dev.herokuapp.com/',
    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.net/RaphaelDDL/mZG83/ (I'm outputting the HTML on the textarea in this example).

RaphaelDDL
  • 4,452
  • 2
  • 32
  • 56
  • new error: Uncaught SyntaxError: Unexpected token : phpproxy-dev.herokuapp.com/:1 parsererrorError: jQuery19107590448246337473_1375193471216 was not called – Himmators Jul 30 '13 at 14:40
  • did you see that I was running greasemonkey, it initiates jquery, where those numbers come from but maybe it's associated? – Himmators Jul 30 '13 at 14:42
  • The error indicates that said function isn't defined on the window, where `window` is whatever jQuery is using as the window. specifying a specific callback shouldn't change the error (other than what is reported as the callback) since the error is coming from the same place. – Kevin B Jul 30 '13 at 14:42
  • @KristofferNolgren Check the fiddle, I've updated it after I had posted. Now it's okay. And about the numbers I've seend they don't come from the JSON, they are jQuery specifics so the numbers might have timestamp. – RaphaelDDL Jul 30 '13 at 14:42
  • Uncaught ReferenceError: jQuery19109166778242215514_1375195463773 is not defined ?callback=jQuery19109166778242215514_1375195463773&url=http%3A%2F%2Fpersoner.eniro.se%2Fresultat%2Fjohn%2Flondon&_=1375195463794:1 (anonymous function) ?callback=jQuery19109166778242215514_1375195463773&url=http%3A%2F%2Fpersoner.eniro.se%2Fresultat%2Fjohn%2Flondon&_=1375195463794:1 parsererrorError: jQuery19109166778242215514_1375195463773 was not called – Himmators Jul 30 '13 at 14:44
  • @KristofferNolgren What jQuery version you using? I'm using 1.9.1 on my fiddle example: http://jsfiddle.net/RaphaelDDL/mZG83/ – RaphaelDDL Jul 30 '13 at 14:48
  • I'm using 1.9.1, in greasmonkey it's loaded like this:// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js – Himmators Jul 30 '13 at 14:49
  • @KristofferNolgren Read this: http://stackoverflow.com/a/859092/684932 You might need to remove and re-install your gm script if you changed the `@require` after installed. Other than that, add `var $ = unsafeWindow.jQuery;` beofre the script (when a page already have jquery, to not have conflict. more info here: http://stackoverflow.com/a/4261831/684932 ) – RaphaelDDL Jul 30 '13 at 14:56
  • Whoever gave -1, please say why, because the fiddle works. OP just added the greasemonkey info later. – RaphaelDDL Jul 30 '13 at 14:58
  • @RaphaelDDL Jquery is woking properly and the site doesn't run another instance of it. (btw, I didn't downvote your answer, I find it excelent!) – Himmators Jul 30 '13 at 15:01
2

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

Himmators
  • 14,278
  • 36
  • 132
  • 223