0

I have a JS function with ajax and I keep getting this error when loading the page: JSON Parse error: Unexpected identifier "array"

I tried looking it up online and didn't see any with the array issue.

$("#imported-list-wrapper").html("");
    $("#processing").show();
    $("#data-for-analysis").hide();
    var selectedCriteriaValues = new Array();
    $.each(criteriaList,function(index,criteria) {
        var item = {"Name" : criteria, "Value" : $("#search-" + criteria).val()};
        selectedCriteriaValues.push(item);
    });
    dateList = new Array();
    
    $.ajax({
        url: "Resources/PHP/GetImportedData.php",
        data: {
            dataset: currentDatasetID,
            conditions: selectedCriteriaValues,
        },                         
        success: function(response) {
            $("#processing").hide();
            // CHANGED
            var data = JSON.parse(response);// ERROR HERE
            importedData = data;
            var items = "";
            if (data.length > 0)
1
  • 1
    What is the response? Commented Nov 16, 2020 at 22:56

2 Answers 2

1

Jquery is being intelligent (most likely) based on the return content type. So the response is already a javascript object and should work without JSON.parse(). See https://api.jquery.com/jquery.ajax/ and find "dataType".

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

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

2 Comments

I changed to var data = response; and the website keeps loading for ever.
I also had a mismatch with dataset: currentDatasetID. I changed and it works!
0

I have changed

.then(response => response.json())

to

.then(response => response.text())

Comments

Your Answer

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