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

  • 3
    `JSON.parse()` **returns** the parsed content. You're calling it and throwing away the result. – Pointy Mar 19 '18 at 21:52
  • Wow I've been trying to fix my code for so long i cant believe that i missed that thanks – Стёпа Кашкаров Mar 19 '18 at 21:54
  • `dataType: "json",` says the response will be auto parsed. You will get an error if you parse already parsed objects. – Taplar Mar 19 '18 at 21:54
  • The response text that goes into the for loop is clearly not parsed – Стёпа Кашкаров Mar 19 '18 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 https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Taplar Mar 19 '18 at 21:58
  • Also the format for success is `Type: Function( Anything data, String textStatus, jqXHR jqXHR )` which does not match what you have. http://api.jquery.com/jQuery.ajax/ – Taplar Mar 19 '18 at 22:00
  • The success is just there for debug the error occurs later in the code – Стёпа Кашкаров Mar 19 '18 at 22:01
  • And if that debugging logic causes an error it can inadvertently affect other things. – Taplar Mar 19 '18 at 22:01
  • I was getting an error in that section of the code but I was kinda ignoring it. Ill structure my ajax requests better next time I post to stack ovetflow but thanks for pointing it out – Стёпа Кашкаров Mar 19 '18 at 22:04

1 Answers1

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

Kim Hogeling
  • 641
  • 6
  • 14