I use this piece of code to asynchronously make a request with React-Native, but it seems like it doesn't get it. The request has been tested separately and the data should be valid.
var data = [{name: 'simon'}];
const req = new XMLHttpRequest();
req.open('GET', testedValidUrl, true);
req.send();
req.onreadystatechange = processRequest;
var name ="";
function processRequest(e) {
if(req.readyState == 4 && req.status == 200){
var response = JSON.parse(req.responseText);
data[0].name = response.name;
}
// setTimeout(()=>{},1000);
}
I thought that maybe it was because of concurrence and that the large array in the real application takes less time to construct than to get the data from the server. Adding setTimeout() did not fix it.
var data = [{name: 'simon'}];
const req = new XMLHttpRequest();
function processRequest(e) {
if(req.readyState == 4 && req.status == 200){
var response = JSON.parse(req.responseText);
data[0].name = response.name;
}
req.onreadystatechange = processRequest;
req.open('GET', testedValidUrl, true);
req.send();
var name ="";
If you set the event handler, (onreadystatechange) after you send/open the request, the readyState has already changed and thus it won't be called.
Related
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://reqbin.com/echo/get/json");
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
console.log(xhr.responseText);
}
};
xhr.send();
let a = xhr.responseText
Why can't I save the value of variable 'a' right away?
I can not get the value {"success":"true"}
how can i get?
Since this request is happening asynchronously your
let a = xhr.responseText
code is executing before the server returns a response
In that case what you can do is place the code that you want to execute inside onreadystatechange event handler, which will execute after server has returned you a response.
let xhr = new XMLHttpRequest();
let a;
xhr.open("GET", "https://reqbin.com/echo/get/json");
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
console.log(xhr.responseText);
a = xhr.responseText
}
};
xhr.send();
How can I best format my POST request for the Cisco API CompressedClientLog?
https://developer.cisco.com/docs/finesse/#!compressedclientlog%e2%80%94post-compressed-log-to-finesse/compressedclientlogpost-compressed-log-to-finesse
CompressedClientLog—Post Compressed Log to Finesse
var data = new FormData();
var logObj = '<ClientLog><logData>This is some logged data for the log file</logData></ClientLog>';
data.append("logData", logObj);
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log("Gadget is now engaged" + this.responseText);
}
});
xhr.open("POST", "https://finesse1.xyz.com/finesse/api/User/finadmin/CompressedClientLog");
xhr.send(data);
Response is:
<ApiErrors>
<ApiError>
<ErrorType>Parameter Missing</ErrorType>
<ErrorData>logData</ErrorData>
<ErrorMessage>Missing logData in the ClientLog object</ErrorMessage>
</ApiError>
</ApiErrors>
Where am I going wrong?
const xhrRequest = new XMLHttpRequest();
xhrRequest.onload = function()
{
dump(xhrRequest.responseXML.documentElement.nodeName);
console.log(xhrRequest.responseXML.documentElement.nodeName);
}
xhrRequest.open("GET", "/website_url.xml")
xhrRequest.responseType = "document";
xhrRequest.send();
I'm trying to request a xml page from a page, but i'm unable to get certain line from xml in javascript. Thank you!
You can easily send requests to other pages with an AJAX http request found here: https://www.w3schools.com/js/js_ajax_intro.asp
Here is an example function:
function SendRequest(){
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if(this.readyState == 4 && this.status == 200){
// Success
}
};
xmlhttp.open("GET", "example.com", true);
xmlhttp.send();
}
Now, about getting a value from the xml document, you can use .getElementsByTagName(). Notice that this is an array of elements so you have to append an index such as [0]
This would go inside the onreadystatechange of the http request
if(this.readyState == 4 && this.status == 200){
let xmlDocument = this.responseXML;
console.log(xmlDocument.getElementsByTagName("TestTag")[0].childNodes[0].nodeValue);
}
So if the xml document had an element like:
<TestTag>Hello</TestTag>
the function would print Hello
I am having trouble storing the response of my XHR request.
Here is the javascript so far:
var req = new XMLHttpRequest;
req.open('get', 'https://jsonplaceholder.typicode.com/todos', true);
req.responseType = 'json';
req.send();
Over at chrome dev tools, i can see that response is the items i want so request should be fine. It is status of 200 and state 4.
The problem becomes when i try to write
var myJSON = req.response;
When i log this to console it responds with "null".
But if i reassign the value through dev tools, to exactly the same value, i get my JSON object.. Can someone please explain to me why and how i can fix this?
try with ready state
var req = new XMLHttpRequest;
req.open('get', 'https://jsonplaceholder.typicode.com/todos', true);
req.responseType = 'json';
req.send();
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.response);
var myJSON = this.response;
}
};
I'm trying to use XMLHttpRequest to get data from two different localhost ports. It will run the request as soon as the page loaded. And I will run a function as soon as it finish getting the data from the 2 ports. But so far, only can get the data from local port 1000. I have check the ports already but no problem. The problem must have been the javascript.
js:
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:1000/data1";
var info1;
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
info1 = jQuery.parseJSON(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
var xmlhttp1 = new XMLHttpRequest();
var url1 = "http://localhost:2000/data2";
var info2;
xmlhttp1.onreadystatechange = function () {
if (xmlhttp1.readyState == 4 && xmlhttp1.status == 200) {
info2 = jQuery.parseJSON(xmlhttp1.responseText);
reload(info1, info2);
}
}
xmlhttp1.open("GET", url1, true);
xmlhttp1.send();
function reload(info1, info2) {
alert("success");
// processing the data
}
Is it that something wrong with the javascript?