Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome 在Chrome扩展中使用jQuery.getJSON_Google Chrome_Jquery_Jsonp - Fatal编程技术网

Google chrome 在Chrome扩展中使用jQuery.getJSON

Google chrome 在Chrome扩展中使用jQuery.getJSON,google-chrome,jquery,jsonp,Google Chrome,Jquery,Jsonp,我需要在chrome扩展中执行跨域请求。我知道我可以通过它,但我宁愿只使用jQuery习惯用法(这样我的javascript也可以作为) 我做的是正常的: $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data) { console.log(data); }); 但

我需要在chrome扩展中执行跨域请求。我知道我可以通过它,但我宁愿只使用jQuery习惯用法(这样我的javascript也可以作为

我做的是正常的:

$.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是否没有将回调函数正确插入文档?我能做些什么来让它工作


(如果我将代码粘贴到chrome控制台中,效果会很好,但如果我将其放在扩展中的page.js中,问题就会出现。)

语法有点不正确。不需要
回调(
位)。这可以完美地工作。在这个StackOverflow页面(包括jQuery)的Chrome的javascript控制台中测试:

如果在文件中指定“api.flickr.com”,则无需使用JSONP回调,即跨域请求的脚本注入样式

例如:

"permissions": ["http://api.flickr.com"],
这在您的代码中应该可以很好地工作。我将删除querystring参数“&jsoncallback”,因为不需要JSONP工作


当前代码不起作用的原因是您的代码正在注入页面DOM,内容脚本可以访问DOM,但不能访问javascript上下文,因此没有方法调用回调。

唉,这些都不起作用,所以我最终通过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>

很多人都知道,谷歌浏览器目前不支持任何方便的GM_uu功能

因此,由于各种沙箱限制(即使使用诸如

我需要一种方法让用户知道我的Greasemonkey脚本何时在Chrome中更新过(因为Chrome也不这么做…)。我提出了一个解决方案,该解决方案记录在这里(并在我的脚本中使用),值得你们中那些想要检查脚本版本的人阅读:


我的印象是这失败了,因为jQuery回调函数是在Chrome扩展的“孤立世界”中创建的,当响应返回时无法访问:

出于各种原因,我使用Prototype和jQuery,但我的快速修复应该很容易解析:

// 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功能:


修正了语法(复制粘贴抱歉)。它在控制台中可以正常工作,但在作为扩展的
page.js
运行时不行。它是
page.js
内容脚本?仅供参考,他说的是CHROME扩展。不在浏览器中。当我这样做时,我得到了以下错误:
端口错误:无法建立连接。接收端不存在。
我在里面包括什么如果我想显示JSON数组中的一个元素,该函数是什么?这对于任何查看的人来说都是正确的答案。因为这对我在TamperMonkey脚本中调用JSONP很有用。谢谢!
chrome_getJSON = function(url, callback) {
  console.log("sending RPC");
  chrome.extension.sendRequest({action:'getJSON',url:url}, callback);
}

$(function(){
  // use chrome_getJSON instead of $.getJSON
});
// 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");
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();