2010-04-12 81 views
6

我需要在Chrome扩展中执行跨域请求。我知道我可以通过message passing,但我宁愿坚持只是jQuery成语(所以我的JavaScript也可以作为<script src="">)。在Chrome扩展中使用jQuery.getJSON

我做正常的:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data) { 
    console.log(data); 
}); 

但在错误控制台我看到:

Uncaught ReferenceError: jsonp1271044791817 is not defined 

jQuery的是没有正确插入回调函数到文档?我能做些什么来完成这项工作?

(如果我将代码粘贴到一个控制台铬,它工作正常,但如果我把它作为page.js中的分机问题出现时。)

回答

8

唉,这些都不起作用,所以我最终通过background.html做了沟通。

background.html

<script src="http://code.jquery.com/jquery-1.4.2.js"></script> 
<script> 
function onRequest(request, sender, callback) { 
    if (request.action == 'getJSON') { 
    $.getJSON(request.url, callback); 
    } 
} 

chrome.extension.onRequest.addListener(onRequest); 
</script> 

的JavaScript/page.js

chrome_getJSON = function(url, callback) { 
    console.log("sending RPC"); 
    chrome.extension.sendRequest({action:'getJSON',url:url}, callback); 
} 

$(function(){ 
    // use chrome_getJSON instead of $.getJSON 
}); 
+0

致谢 http://developer.chrome.com/extensions/declare_permissions - 解决我的问题,太:) – 2011-06-04 23:27:49

+0

当我做到这一点,我得到这个错误:'端口错误:无法建立连接。接收结束不存在。 – 2014-03-11 08:35:23

+0

如果我想显示JSON数组中的元素,那么我在函数内包含什么? – wishman 2015-07-24 11:56:40

0

的语法是有点过。没有必要使用callback(位。这工作完美无瑕。经测试,在Chrome浏览器的这个StackOverflow的页面上的JavaScript控制台(包括jQuery的):

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data) { 
    console.log(data); 
}); 
+1

固定的语法(复制和粘贴抱歉)。它可以在控制台中正常工作,但是不能以分机的'page.js'运行 – 2010-04-12 04:35:55

+0

'page.js'是内容脚本吗? – 2010-04-12 06:04:50

+0

是的,是的。那是问题吗? – 2010-04-12 06:45:45

3

如果您在manifest.json文件中指定“api.flickr.com”你不会需要使用JSONP回调,脚本注入跨域请求的风格。

例如:

"permissions": ["http://api.flickr.com"], 

这应该在你的代码做工精美。因为没有JSONP工作需要,我会删除查询字符串参数“& jsoncallback”。

你目前的代码不工作的原因是你的代码注入页面DOM,内容脚本可以访问DOM但没有访问javascript上下文,所以没有方法在回调中调用。

+0

这是任何人查看的正确答案。 因为 http://developer.chrome.com/apps/contentSecurityPolicy – BradLaney 2014-03-14 19:43:19

0

正如你们许多人都知道,谷歌Chrome浏览器不会在此刻支持任何的得心应手GM_功能。

因此,它是不可能做由于

我需要一种方式,当我的Greasemonkey脚本已得到更新,用户可以知道各种沙盒限制(即使使用很大的工具,如James Padolsey's Cross Domain Request Script)跨站点AJAX请求在Chrome中(因为Chrome并没有这么做......)。我想出了这是在这里(和在我Lighthouse++脚本中使用)记录了解决方案,值得一读。对于那些你想要的版本检查脚本:

http://blog.bandit.co.nz/post/1048347342/version-check-chrome-greasemonkey-script

2

我的印象是,这个失败,因为jQuery的回调函数被Chrome扩展的“分离的世界”中创建,是不可访问时响应回来:

http://code.google.com/chrome/extensions/content_scripts.html#execution-environment

我使用的原型和jQuery由于种种原因,但我速战速决应该很容易RSE:

// Add the callback function to the page 
s = new Element('script').update("function boom(e){console.log(e);}"); 
$$('body')[0].insert(s); 

// Tell jQuery which method to call in the response 
function shrink_link(oldLink, callback){ 
    jQuery.ajax({ 
     type: "POST", 
     url: "http://api.awe.sm/url.json", 
     data: { 
      v: 3, 
      url: oldLink, 
      key: "5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9", 
      tool: "mKU7uN", 
      channel: "twitter" 
     }, 
     dataType: "jsonp", 
     jsonpCallback: callback 
    }); 
} 

// And make it so. 
shrink_link('http://www.google.com', "boom"); 

或者你可以尝试使用扩展XHR能力:

http://code.google.com/chrome/extensions/xhr.html

var xhr = new XMLHttpRequest(); 
xhr.open("GET", "http://api.example.com/data.json", true); 
xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4) { 
    // JSON.parse does not evaluate the attacker's scripts. 
    var resp = JSON.parse(xhr.responseText); 
    } 
} 
xhr.send(); 
+0

这适用于我在TamperMonkey脚本中调用JSONP。谢谢! – 2014-03-27 05:29:44