You already assign a parameter from the response to the variable value . But apparently you have a completely different question. You are making an asynchronous request, it looks most likely for you like this:
var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { //alert(xhr.responseText); var jsonResponse = JSON.parse(xhr.responseText); value = jsonResponse.id; } else { console.error(xhr.statusText); } }; xhr.open("GET",url, true); xhr.send();
The request is sent at the moment xhr.send () , and the function xhr.onreadystatechange will work when the answer comes. When this happens by and large is not known. To use the data from the response, you need to call an action in the callback function that should use the value variable, for example:
var value; function useValue() { // Делаете то что вам нужно } xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { //alert(xhr.responseText); var jsonResponse = JSON.parse(xhr.responseText); value = jsonResponse.id; useValue(); } else { console.error(xhr.statusText); } };