簡體   English   中英

修復 JSON 解析錯誤:意外的標識符“數組”

[英]Fix JSON Parse error: Unexpected identifier "array"

我有一個帶有 ajax 的 JS 函數,我在加載頁面時不斷收到此錯誤: JSON Parse error: Unexpected identifier "array"

我嘗試在網上查找它並沒有看到任何有關陣列問題的信息。

$("#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)

基於返回內容類型,Jquery 是智能的(最有可能的)。 所以響應已經是一個 javascript 對象,應該可以在沒有 JSON.parse() 的情況下工作。 請參閱https://api.jquery.com/jquery.ajax/並找到“dataType”。

dataType(默認值:Intelligent Guess(xml、json、script 或 html)) 類型:String 您期望從服務器返回的數據類型。 如果沒有指定,jQuery 將嘗試根據響應的 MIME 類型推斷它(XML MIME 類型將產生 XML,在 1.4 中 JSON 將產生一個 JavaScript 對象,在 1.4 腳本中將執行腳本,其他任何類型都將以字符串形式返回)。 可用類型(以及作為成功回調的第一個參數傳遞的結果)是:

我變了

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM