0

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?

Daniel
  • 34,125
  • 17
  • 102
  • 150
  • 1
    i think `return true;` should be on the end of the function to keep the port open and not in the callback function – Damian Dec 14 '18 at 07:44
  • From the signature `(request, sender, sendResponse)` this is a handler for `onMessage`. So the problem must be in how you register that handler and which part of the extension this is in. As is, there's not enough information in the question to diagnose your problem. – Xan Dec 14 '18 at 08:52
  • Xan, yes. The user interfaces with the popup script. The popup script sends a message to the background script. The background script does the xhr to the server. The background script gets the response and sends it to the popup script to give the user feedback. – Richard Bernstein Dec 15 '18 at 14:03

1 Answers1

3

The issue you are seeing may be due to a Preflight operation. Something you would see with servers that handle CORS. The browser sends two requests, one using OPTIONS method and the second using POST. This is based on your server configuration

if you un-comment

XHR.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

you may see only one request, but that may not give you the desired response.

You may want to handle your preflight manually in codeigniter using

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
// send OK without processig
}}
Daniel
  • 34,125
  • 17
  • 102
  • 150
  • Hi @Daniel , thanks for the answer! Do you have any idea how to disable preflight via nginx? (I have jenkins sending double requests - also users sending me doubled feedback via XHR) – Barney Szabolcs Jan 10 '20 at 14:07
  • it's a matter of setting the OPTIONS to return a 2** response and not being handled by the application. You can have a look at this example. https://gist.github.com/michiel/1064640 – Daniel Jan 10 '20 at 15:51
  • 1
    aah, now I get it! (2** = 204 in your code, first I didn't realize) Thank you, @Daniel! – Barney Szabolcs Jan 11 '20 at 21:40