0

I went to whateverorigin.com to generate this line of javascript:

$.getJSON('http://whateverorigin.org/get?url=' +
  encodeURIComponent('http://google.com') + 
  '&callback=?', function(data){ alert(data.contents); });

I put that in place of the XMLHttpRequest I was using before, and nothing happens. Chrome says I have an Uncaught Reference Error: jQuery110201568311753217131_1395117728011 is not defined. I must not be setting everything up right, what else do I need to do besides put that line with the relevant url in my JavaScript?

aquemini
  • 950
  • 2
  • 13
  • 32
  • what version of jquery are you using? – Patrick Evans Mar 18 '14 at 04:52
  • @PatrickEvans jQuery version 1.10.2 – aquemini Mar 18 '14 at 04:53
  • @aquemini : for same domain you don't need to use '&callback=?', even though if use that, you must resend same callback with your response – Laxmikant Ratnaparkhi Mar 18 '14 at 04:55
  • Interestingly, you can run that code on page and it works! :-) – go-oleg Mar 18 '14 at 04:55
  • Can you make a [jsfiddle](http://jsfiddle.net) that recreates your problem, as http://jsfiddle.net/968TS/ the basic setup in this fiddle shows it working – Patrick Evans Mar 18 '14 at 04:56
  • @PatrickEvans http://jsfiddle.net/968TS/4/ i think that captures it – aquemini Mar 18 '14 at 05:02
  • @aquemini - this variation seems to work: http://jsfiddle.net/jfriend00/HPTFm/. You have to include "callback=?" with `$.getJSON()` for it to know that you want to use JSONP which is required for cross origin. – jfriend00 Mar 18 '14 at 05:14
  • @jfriend00 that does work in fiddle, but not in the browser. when i add "callback=?" back in, i get the uncaught reference error i mentioned in the original post. any reason that could be? – aquemini Mar 18 '14 at 05:18
  • @aquemini - then something else (that you haven't disclosed to us) is wrong with your regular page. jQuery knows how to process that request if you set it up right. – jfriend00 Mar 18 '14 at 05:19
  • @jfriend00 here's the fiddle with my exact code: http://jsfiddle.net/HPTFm/7/...same problem, works in fiddle, not in my chrome extension. i even put an "alert(team)" right before the call to .getJSON and that works fine in Chrome – aquemini Mar 18 '14 at 05:23
  • Hmmm, you didn't say until just now that this is a Chrome extension. I wonder what is different about that environment? Perhaps it has a different global variable space and thus the JSONP callback can't find the global function it's supposed to call? – jfriend00 Mar 18 '14 at 05:24
  • Yep, that was it. See my answer below for details. – jfriend00 Mar 18 '14 at 05:33

1 Answers1

1

The issue is that the Chrome extension has it's own sandboxed global variables and thus the global callback function that a JSONP request uses doesn't work when made from a Chrome extension.

See JSONP request in chrome extension, callback function doesn't exist? and JSONP communication in a Google Chrome extension and Using jQuery.getJSON in Chrome Extension for further details. This question is probably a dup of that one.

If you search Google for "JSONP Chrome Extension", you find many discussions of this issue with several different resolutions depending upon the exact circumstances.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @aquemini - yeah, the missing piece of info was that you're in a Chrome Extension. The Google search was quite simple once we knew that to be the relevant difference. – jfriend00 Mar 18 '14 at 05:33