Welcome Guest, Not a member yet? Register   Sign In
returning a message from a background script
#1

(This post was last modified: 03-30-2018, 05:26 PM by richb201.)

I have managed to get a response back from the server back to the background script in my Chrome extension that it is communicating with. Now I need to get a message back to the popup so I can repopulate a table. Here is the code that gets the response back from the server in a function called send_to_backend(). This function is being called when the within and onload when the readyState=200 is satisfied. 


Code:
    function send_to_backend()
    {
    

    myArray["email"]=jsonResponse["email"];
    var json = JSON.stringify(myArray);
     var url = 'http://localhost/Subit_backend/register';  
    
     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.onload = function () {   //response will go here
            if(xhr.status === 200) {
                var jsonResponse = JSON.parse(xhr.responseText);
                // Log the whole json object
                console.log(jsonResponse);
                // Access 'success' from the object
                console.log(jsonResponse.success);
                sendResponse({response: jsonResponse.success});   <<< here
                //return(jsonResponse);
                
            }
     // Request finished. Do processing here.
      
   };
The problem is that sendResponse is not recognized and is causing an error. 

  1. "response is not defined"

  2. stack:"ReferenceError: response is not defined↵    at eval (eval at xhr.onload (chrome-extension://bdjgnodlhfmhghjhbkkkaaammfocdpib/background.js:108:5), <anonymous>:1:1)↵    at XMLHttpRequest.xhr.onload (chrome-extension://bdjgnodlhfmhghjhbkkkaaammfocdpib/background.js:108:5)"
      
I wonder of the problem is that the sendResponse is not actually within the  addlistener. It is in a separate function , send_to_backend(), to make it more manageable.
proof that an old dog can learn new tricks
Reply


Messages In This Thread
returning a message from a background script - by richb201 - 03-30-2018, 05:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB