This is the XMLHttpRequest object. When I put it on cosole.log
it gives the following response:
console.log(this.responseText);
"[
{
"url": "https://api.github.com/gists/c7c0df592e99c0c34b99",
"forks_url": "https://api.github.com/gists/c7c0df592e99c0c34b99/forks",
"commits_url": "https://api.github.com/gists/c7c0df592e99c0c34b99/commits",
"id": "c7c0df592e99c0c34b99",
"git_pull_url": "https://gist.github.com/c7c0df592e99c0c34b99.git",
"git_push_url": "https://gist.github.com/c7c0df592e99c0c34b99.git",
"html_url": "https://gist.github.com/c7c0df592e99c0c34b99",
"files": {
"config.json": {
"filename": "config.json",
"type": "application/json",
"language": "JSON",
"raw_url": "https://gist.githubusercontent.com/anonymous/c7c0df592e99c0c34b99/raw/70489beaa4953f89fc8848195371da6eca76164c/config.json",
"size": 17911
}
},
"public": true,
"created_at": "2015-04-26T20:34:11Z",
"updated_at": "2015-04-26T20:34:11Z",
"description": "Bootstrap Customizer Config",
"comments": 0,
"user": null,
"comments_url": "h"[…]
But when I try to use JSON.parse
on it it gives me an error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
.
gists = JSON.parse(this.reponseText)
im supposed to be using this api https://developer.github.com/v3/gists/ and its supposed to return valid json according to that documentation
Is the above data that was returned by the website not valid JSON? Or am I supposed to use a different function other than JSON.parse
? Or what is going on? Please help.
full pastebin here: http://pastebin.com/BWttNtXP
Try this :)
console.log(this.responseText);
var txt = this.responseText.trim("\"");
gists = JSON.parse(txt);
this way your pastebin html works ok
Update: Problem Solved
The root problem is a typographical error in the code. Can you spot it?
gists = JSON.parse(this.reponseText);
Corrected:
gists = JSON.parse(this.responseText);
OP's code code works normally once this change is made.
Well, anyone that's coded for awhile knows how easy it is to waste a lot of time on something as simple as a missing bracket or semicolon. And when you finally find it ... D'oh!
Original Post:
There doesn't appear to be anything wrong with the json source as the below code is able to pull and process the data fine. Click "Run Code Snippet" to view.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JSON</title>
</head>
<body>
<h1 style="background-color:steelblue; color:white; padding:5px;">JSON DATA TEST</h1>
Raw Data:
<textarea id="output" style="width: 100%; height: 40em;padding:0.5em; border:1px black solid;"></textarea>
<script type="text/javascript">
// synchronous request for testing only.
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.github.com/gists/public', false);
xhr.send();
document.getElementById('output').value = xhr.responseText;
try {
var data = JSON.parse( xhr.responseText );
alert( 'SUCCESS:
' + data[0].forks_url );
}
catch(e){ alert( 'ERROR:
' + e.description ); }
</script>
</body>
</html>
</div>