I am new in Java and JavaScript and I was looking for a way to take an HTTPResponse, parse the .responseText using the .indexOf method so as to take the property I need and after that display this value in a cell of a table row I have created in an html page. Is there a way to replace this cell every time I get the httpresponse?
Thanks a lot.
var xHRObject = new XMLHttpRequest();
var url = "http://meteovarkiza.gr/";
xHRObject.open("GET", url, true);
xHRObject.send();
xHRObject.onreadystatechange = function () {
if (xHRObject.readyState==4 && xHRObject.status==200) {
var response = xHRObject.responseText;
var startIndex = response.indexOf("Temperature");
startIndex += 70;
var subResponse = response.slice(startIndex, startIndex + 4);
alert(subResponse);
}
}
Your question isn’t very clear, but you seem to be saying that you are successfully getting the particular value you need out of the response text and into the
subResponsevariable, but you don’t know how to put that value into a particular element on your page.If so, the simplest way is to give that element an
id:And then do this in your JS:
In your question title you said:
…but you seem to have figured that out. If you have no control over the response format then I guess a combination of regex tricks and/or
.indexOf()and.slice()(or.substr()might be better since in your case you always specify 4 characters) is the way to do it.If you can set the format of the response you are probably much better off returning a JSON string, because then you can use
JSON.parse()to get an object with directly accessible properties – no more messing around with string manipulation.