Darky
Darky

Reputation: 1

Remove a line from JSON parse

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?

Upvotes: -2

Views: 1145

Answers (1)

Akinjide
Akinjide

Reputation: 2763

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());

Upvotes: -2

Related Questions