My a function in the background task for my Chrome Extension is below. It works fine and sends the json data to the server. The server receives it and handles it fine too. The problem is that the server immediately sees another same request. The browser at this point gets a "Uncaught SyntaxError: Unexpected end of JSON input" error.
function send_to_logger_survey(request, sender, sendResponse)
{
var myArray=[];
var jsonResponse2;
myArray.push(email);
myArray.push(request.values); //contains chosen values
var json = JSON.stringify(myArray);
var url=szHost;
url=url.concat('/Subit_backend/logger_survey');
var xhr = new XMLHttpRequest();
xhr.onerror = function() { alert('error'); };
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("X-Requested-With",'xmlhttprequest');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.onload = function (){
if((xhr.status === 200)&&(xhr.readyState===4)) {
var jsonResponse2 = JSON.parse(xhr.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.
}
};
xhr.send(json);
}
I don't "thinK" that my javascript code is actually making two calls. I think it maybe that after my Codeigniter code gets and handles the request, that a second copy of the buffer is being sent. Or the code above is sending two copies of the same buffer? Any idea how to debug this problem?