Pau*_*jan 6 jquery jsonp google-chrome
我需要在Chrome扩展程序中执行跨域请求.我知道我可以通过消息传递,但我宁愿坚持只是jQuery习语(所以我的JavaScript也可以作为一个<script src="">).
我做的很正常:
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data) {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
但在错误控制台中我看到:
Uncaught ReferenceError: jsonp1271044791817 is not defined
Run Code Online (Sandbox Code Playgroud)
jQuery没有正确地将回调函数插入到文档中吗?我能做些什么来完成这项工作?
(如果我将代码粘贴到chrome控制台中,它可以正常工作,但如果我把它作为page.js放在扩展名中就会出现问题.)
唉,这些都没有,所以我最终通过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>
Run Code Online (Sandbox Code Playgroud)
Java脚本/ 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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12452 次 |
| 最近记录: |