0

I am working on my webdesign assignment based around ajax. I am having a problem with the JSON.parse() function. The problem goes as follows, I perform a get request on my JSON database using ajax:

artist_list = $.ajax({
    dataType: "json",
    async: false,
    method: "GET",
    url: "/database/artists.json",
    error: function (xhr) {
        console.log("AJAX error:", xhr.status);
    },
    success: function(xhr, responseText) {
        console.log("Ajax operation succseded the code was:", xhr.status);
        console.log("This is the output", responseText);
        response = responseText;
        console.log("Parsing artist list");
        JSON.parse(response);
        console.log("Artist list parsed");
    },
    done: function(data, textStatus, jqXHR){
        console.log("data:", data);
        console.log("Response text",jqXHR.responseText);
    }
})

The console log of the responseText matches what the JSON file I wanted but, when I run response text through a for loop the log returns the JSON file char by char:

artist_list = artist_list.responseText;
JSON.parse(artist_list);
console.log(artist_list);
for(var i = 0; i < 1; i++) {
    console.log("generating artist,", i);
    console.log("which is:", artist_list[i]);
    index = parseInt(i);
    index = new artist_lite(artist_list[i]);
    artist_lite_dict.push(index);
}

The console returns:

generating artist, 0
which is: {     

The list is limited to one because of the lenght of the JSON object which I am trying to pass through. The JSON can be found here https://github.com/Stephan-kashkarov/Collector/blob/master/main/database/artists.json along with the whole code for testing purposes.

Thanks for your help! - Stephan

9
  • 3
    JSON.parse() returns the parsed content. You're calling it and throwing away the result. Commented Mar 19, 2018 at 21:52
  • Wow I've been trying to fix my code for so long i cant believe that i missed that thanks Commented Mar 19, 2018 at 21:54
  • dataType: "json", says the response will be auto parsed. You will get an error if you parse already parsed objects. Commented Mar 19, 2018 at 21:54
  • The response text that goes into the for loop is clearly not parsed Commented Mar 19, 2018 at 21:55
  • 1
    You should also avoid using async false, not only due to performance issues, but it has also been removed in the latest version because of the UX issues. Which would relate this question also to stackoverflow.com/questions/14220321/… Commented Mar 19, 2018 at 21:58

1 Answer 1

1

You need to save the result of JSON.parse(artist_list); into some variable. I guess in your code you would like to solve it like this:

artist_list = JSON.parse(artist_list);

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

1 Comment

Yeah some this and some minor changes fixed my code thanks!

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.