正在通过XMLHttp

时间:2018-12-25 19:05:17

标签: google-chrome google-chrome-extension

我之前曾尝试问过这个问题,但这次我将尝试更具描述性。我有一个Chrome扩展程序弹出窗口。当用户想要将一条消息从我的外部记录到我的后端时,请按“提交”。在我的后台任务中,我使用了onMessage.addListener。当该后台侦听器收到request.task == "log_results_survey"(这意味着用户在弹出任务中点击了Submit)时,它将运行:

    if (request.task =="log_results_survey"){
          send_to_logger_survey(request, sender, sendResponse);
          console.log("sent to logger_survey\n");
          };

Send_to_logger_survey(也在后台任务中)显示在下面:

    function send_to_logger_survey(request, sender, sendResponse)
    {
    var myArray=[];
    var jsonResponse2;
    var d=new Date(); 

    myArray.push(email);
    myArray.push(request.values);  //contains chosen values
    myArray.push(Math.random());   //push random number
    console.log("sent to server\n");
    var json = JSON.stringify(myArray);
    var n = json.length;   //this must be less than 1472 to avoid fragmentation
    var url=szHost;  
    url=url.concat('/Subit_backend/logger_survey');
    var xhr2 = new XMLHttpRequest();
    xhr2.onerror = function() { alert('error'); };
    xhr2.open('POST', url, true);

    xhr2.setRequestHeader("Content-type", "application/json")
    xhr2.setRequestHeader("X-Requested-With",'xmlhttprequest');
    xhr2.setRequestHeader("Access-Control-Allow-Origin", "*");

    xhr2.onload = function (){

            if((xhr2.status === 200)&&(xhr2.readyState===4)) {
                if (xhr2.responseText==="")
                    var jsonResponse2="empty";
                else
                    var jsonResponse2 = JSON.parse(xhr2.responseText);
                sendResponse({task: jsonResponse2});
              return true; //The sendResponse callback is only valid if used synchronously, or if the event handler returns true to indicate that it will respond asynchronously.   
            }
    };
    xhr2.send(json);
    }

我所看到的(在服务器上以及通过Wireshark)是从客户端(49814和49815)的两个连续端口发送相同缓冲区的两个副本!您可以在缓冲区41和42中看到两者。我知道如何将其恢复为缓冲区的一个副本吗?

奇怪的细节:当我查看由主机接收的两个随机数时,它们在两个不同的缓冲区中是不同的。看来send_to_logger_survey()运行了两次,但是使用调试器,我只看到它在断点处停止一次。 wireshark capture

0 个答案:

没有答案