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.
- "response is not defined"
- 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