I am working with JIVE JSON RESTFul API and on their API they have a security
(I am referring to the throw 'allowIllegalResourceCall is false.'; )
throw 'allowIllegalResourceCall is false.';
{
"id" : "52104",
}
I am using this code to try to PARSE IT:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("topdisplay").innerHTML = myObj.id;
}
};
xmlhttp.open("GET", "http://mysite/API/",true);
xmlhttp.send();
And I am getting an error because of that starting line.
I looked everywhere to try finding a solution to skip that to the JSON PARSE would work but I can't seem to find a way that works to do it.
Also I know that the parsing code works because when I remove the first line it works perfectly.
Any help?
JIVE REST API introduced this to help prevent against JSON Hijacking back when web browsers were susceptible.
You'll need to first find {, and do a substring from that position to the end of the string.
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = this.responseText
var myObj = JSON.parse(this.responseText.substring(this.response.indexOf('{')));
document.getElementById("topdisplay").innerHTML = myObj.id;
}
};
xmlhttp.open("GET", "http://mysite/API/",true);
xmlhttp.send();
NOTE:
using RegEx, you'll need to find the first line with throw and ending in a semi-colon, if found replace with an empty string.
JSON.parse(response.replace(/throw.*;/, "").trim());
Related
I would like my program to import an JSON file, using the data to create an array-list then access the array-list so that I can display it on the web page.
I've tried using JSON.parse() but it only works when I do something like JSON.parse('[{"shape":"polygon"},{"shape":"square"}]); It doesn't work for parsing a JSON file that is not declared inside. My JSON file is saved to my desktop. I'm new to JavaScript and importing files so anything would be helpful!
I've tried using:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObject = JSON.parse(this.responseText);
}
};
xmlhttp.open("GET", "PATIENT51.txt", true);
xmlhttp.send();
I keep getting errors with: JSON.parse(this.responseText);
JSON.parse Error: Invalid character at position:5 problemlist1.html(3,5)
You can just have the log in xmlhttp scope and it'll work.
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
console.log(JSON.parse(this.responseText))
}
};
xmlhttp.open("GET", "PATIENT51.txt", true);
xmlhttp.send();
EDIT: seen your edit, in addition, make sure that your JSON has the right format:
{
"key1" : 1,
"key2" : 2,
"key3" : 3
}
I'm trying to write some java script code that will (if the file exists) add its content to the web page, if it doesn't exist then it will check if the next file exists. I currently have it displaying the data if the file exists, however throws
GET http://localhost:8080/temp1.xml 404 (Not Found)
xmlhttp.onreadystatechange # friends.html:56
XMLHttpRequest.send (async)
(anonymous) # friends.html:95
if the file doesn't exist.
I've already tried adding i try catch around different parts but none of them seem to stop the error from occurring. ive also been trying to find a decent guide on how to handle not found errors but cant seem to find one that would work in my case.
this is the code I have without the try excepts.
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
JSObj = JSON.parse(this.responseText);
console.log(JSObj.friends.length);
for (i = 0; i < JSObj.friends.length; i++) {
name = JSObj.friends[i].name;
xhttp1.open("GET", "temp"+ i +".xml", true);
xhttp1.send();
};
};
};
this is the code I have with the try excepts
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
JSObj = JSON.parse(this.responseText);
console.log(JSObj.friends.length);
for (i = 0; i < JSObj.friends.length; i++) {
name = JSObj.friends[i].name;
try{
xhttp1.open("GET", "temp"+ i +".xml", true);
xhttp1.send();
}catch(err){
console.log(err);
};
};
};
};
the output I'm getting is a blank page due to the error stopping the code I have working from working which is when the error above is output. I expect that it would just skip that file and go onto the next one.
That's not the way XMLHttpRequest works. Perceive the difference you're treating the call that run that code and what you're expecting open() method to do.
First you have asynchronously made a call to a URL you have not mentioned. When this process returned it called the onreadystatechange event which runned the code you posted in the question.
Then you expect open() method to function differently and use a try/catch. See the difference ?
See that you have setted the async parameter to true. So you are opening a whole new call on another object called xmlhttp1. With this 2nd call you create another XMLHttpRequest request that should be addressed by another onreadystatechange method to process it.
You can read more on using XMLHttpRequest here:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
More or less like this :
NOT TESTED PSEUDO-CODE
xmlhttp1 = new XMLHttpRequest();
xmlhttp1.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// whatever you want to do with the call to
// xhttp1.open("GET", "temp"+ i +".xml", true);
}
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
JSObj = JSON.parse(this.responseText);
console.log(JSObj.friends.length);
for (i = 0; i < JSObj.friends.length; i++) {
name = JSObj.friends[i].name;
xhttp1.open("GET", "temp"+ i +".xml", true);
xhttp1.send();
};
};
};
Please adapt this code. It's just to illustrate what I mean and probably will not work this way.
I am making a simple XML parser in javascript using the XMLHttpRequest object, and i am having trouble returning only the tags I want. For example, for this xml file I would like to access and print the variant array (1st one), however my current code is not working.
JavaScript
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
function run() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://www.deadstock.ca/collections/adidas/products/adidas-equipment- support-adv-core-black-24.xml", true);
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
console.log(xhttp.responseText.hash.variant[0]);
}
}
}
run();
However, this works (returns everything)
console.log(xhttp.responseText);
I am currently running the file using node.
any idea with this is not working?
var xmlhttp = new XMLHttpRequest();
var url = "http://content.warframe.com/dynamic/worldState.php";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
}
everytime i get status = 0.
i use the same method to get my script working with the twitch one, but can't get this one working.
thank for your help guys.
edit : ok thanks any idea what can i do to get the data from this site then?(if someone have any idea it will be awesome. as you can guess i can't edit the page who provide the date, but i know some people are able to get the JSON data) .
I am reading a large sitemap.xml file using the following code:
<div id="urls"></div>
<script type="text/javascript" language="javascript">
var xmlhttp;
if(window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState==4 && xmlhttp.status==200) {
loc = xmlhttp.responseXML.documentElement.getElementsByTagName("loc");
for(i=0;i<loc.length;i++) {
document.getElementById("urls").appendChild(document.createTextNode( loc[i].firstChild.nodeValue ));
document.getElementById("urls").appendChild(document.createElement("br"));
}
document.getElementById("urls").innerHTML = table;
}
}
xmlhttp.open("GET", "sitemap.xml", true);
xmlhttp.send(null);
</script>
When reading an XML file with more than 1000 lines it will error with the following details:
Uncaught TypeError: Cannot read property 'documentElement' of null (index):xmlhttp.onreadystatechange
Any ideas on how I can get around this? Tried a few things which I have found online, but was unable to work it out properly.
Many thanks for your help.
You must check whether responseXML is null. That happens if the XML file the server returned has parsing errors.
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function () {
var locs, i, urls = document.getElementById("urls");
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
if (this.responseXML) {
locs = this.responseXML.documentElement.getElementsByTagName("loc");
for (i = 0; i < locs.length; i++) {
urls.appendChild(document.createTextNode(locs[i].firstChild.nodeValue));
urls.appendChild(document.createElement("br"));
}
// What is that line? I've taken it from your code, but it can't be right.
urls.innerHTML = table;
} else {
// responseXML is null if the response has XML parsing errors
alert("The server did not send a proper sitemap file.");
}
}
};
xmlhttp.open("GET", "sitemap.xml");
xmlhttp.send();
Also, as shown above, you should really never use synchronous requests. The whole point of having an event-handing callback function like onreadystatechange is to be able to make asynchronous requests.
There also is a big red warning on the MDN about that: https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest#onreadystatechange
Lessons to learn:
Never use objects without a prior check if they are valid.
Never use synchronous HTTP requests from JavaScript.