将chrome.runtime.onemessage与Jquery ajax一起使用时出错

将chrome.runtime.onemessage与Jquery ajax一起使用时出错,jquery,ajax,google-chrome,google-chrome-extension,Jquery,Ajax,Google Chrome,Google Chrome Extension,以前我有代码: $.get(url, function(data){ var $response = $(data); overallRating = $response.find("div.grade").eq(0).html(); arr[pos].rating = overallRating; 为了得到我想要的数据。这在很大程度上起作用,但我无法将值分配给arr[pos]。我发现,由于$.get()是异步的,所以我可以使用chrome.r

以前我有代码:

 $.get(url, function(data){
        var $response = $(data);
        overallRating = $response.find("div.grade").eq(0).html();
        arr[pos].rating = overallRating;
为了得到我想要的数据。这在很大程度上起作用,但我无法将值分配给arr[pos]。我发现,由于$.get()是异步的,所以我可以使用chrome.runtime.onmessage和.sendMessage并使用回调函数访问该值。所以我在我的背景脚本tabcheck.js中想到了这个:

chrome.runtime.onMessage.addListener(function(message, send, callback){ 
console.log("message recieved");
$.get(send.url , function(data){                                            
            console.log(data);
            callback(data);                                 
    });

return true; //https://stackoverflow.com/questions/17246133/contexts-and-methods-for-communication-between-the-browser-action-background-sc


});
以及我的popup.js页面中的sendmessage:

chrome.runtime.sendMessage('message', {url: url } , function(data) {
            console.log(data);
            var $response = $(data);
            overallRating = $response.find("div.grade").eq(0).html();
            arr[pos].rating = overallRating;
    });
我认为发生的事情是,在onmessage中,url没有被访问。我记得得到了一个类似于的错误,尽管我不记得这是否是正确的错误。数据返回时未定义

那么,第一个和第二个之间发生了什么变化,使得新函数无法再访问url了?我怎样才能修好它?我的目标是将值overAllRating转换为arr[pos].rating

这是我的舱单,如果有必要的话:

{
 "manifest_version": 2,

  "name": "",
  "description": "n",
 "version": "1.0",

"background":{
    "scripts": ["jquery-2.1.3.min.js","tabcheck.js"],
    "persistent": false
},
"permissions":[
    "tabs",
    "https://www.myurl1/*",
    "http://www.myurl2/*",

     ],


 "browser_action": {

    "default_icon": "icon.png",
    "default_popup": "uv.html",
    "default_title": ""

 },

"content_scripts": [
    {
    "matches":["https://www.myurl/*"],
    "js":["jquery-2.1.3.min.js","main.js"]

    }

]


}
免责声明 这个答案是关于修复代码中的错误,而不是逻辑中的错误。您的代码可以工作,但无论如何都不是预期的。请看答案的结尾

修复消息传递代码 这是发送消息的错误格式

您可以看到,但通常需要两个参数:

chrome.runtime.sendMessage(message, callback);
其中,
消息
是任何东西,只要它是JSON可序列化的

如果添加字符串作为第一个参数,它将成为三参数形式,并被解释为交叉扩展消息,第一个参数为目标ID

Chrome试图找到ID为
“message”
的扩展,但失败,并抛出“接收端不存在”错误


传递给侦听器的消息如下所示:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse {
  // message is the original message
  // sender is an object containing info on who sent the message
  // sendResponse is a function to be called
});
您正在尝试使用第二个参数:

chrome.runtime.onMessage.addListener(function(message, send, callback){ 
  console.log("message recieved");
  $.get(
    send.url, // <-- This is wrong
    function(data) {                                            
      console.log(data);
      callback(data);                                 
    }
  );

  return true;
});

现在真正的问题是 现在我们已经修复了这个错误,这对您毫无帮助

您尚未“修复”
$的异步性质。获取
。现在涉及到两层异步性:AJAX和消息传递。您的作业仍然无法按预期工作,并且没有足够的代码来修复这一问题

您可能遇到以下两个问题之一:

  • (如果您试图在此代码之后使用
    arr

  • (如果您认为您的
    pos
    行为滑稽)


我建议仔细研究这两个经典问题。在这里,您根本不需要消息传递,它解决不了任何问题。

我知道我的循环工作正常,因为在我进入ajax之前它就工作了。我以前确实看到了第一个异步答案,我想我理解了,但显然不理解。我再看一眼,试试新的。谢谢你的详细回答。我必须使用$.ajax吗?或者我可以保留$.Get吗?如果你有不同的解决方案或者有更多的细节,可以将其作为另一个答案发布。
chrome.runtime.onMessage.addListener(function(message, send, callback){ 
  console.log("message recieved");
  $.get(
    send.url, // <-- This is wrong
    function(data) {                                            
      console.log(data);
      callback(data);                                 
    }
  );

  return true;
});
chrome.runtime.sendMessage(
  {url: url}, // This will be the message
  function(data) {/*...*/}
);
chrome.runtime.onMessage.addListener(function(message, sender, callback){ 
  console.log("message recieved");
  $.get(
    message.url,
    function(data) {                                            
      console.log(data);
      callback(data);                                 
    }
  );
  return true;
});