0

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

7
  • It's not valid JSON is why... how is it being produced? Can we see that code? Commented Apr 26, 2015 at 20:45
  • 2
    You need to replace starting quotes to single for start, because your inner quotes are double. Commented Apr 26, 2015 at 20:46
  • 2
    @ChavdarSlavov — That's possibly an artefact of the viewer that JSON was copy/pasted from. Commented Apr 26, 2015 at 20:47
  • its copied from the console of the firefox browser Commented Apr 26, 2015 at 20:48
  • 1
    Other than the string quote the JSON is perfectly valid, can you give us a reference to the full json? Commented Apr 26, 2015 at 20:51

2 Answers 2

3

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:\n' + data[0].forks_url );
	}
	catch(e){ alert( 'ERROR:\n' + e.description ); }
</script>
</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

Try this :)

    console.log(this.responseText);
    var txt = this.responseText.trim("\"");
    gists = JSON.parse(txt);

this way your pastebin html works ok

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.