2012-01-03 70 views
5

我收到 「System.ArgumentException:無效的JSON原始:頁次」 當我在下面的代碼返回 「SDATA」:System.ArgumentException:無效JSON的原始錯誤

function getPageData() { 
pagenum = parseInt(eSc("#resultsBtn").attr("data-pagenum")); 
if (pageName === "Home") { 
    scrollPath = "/Home/GetResults/"; 
    sdata = { "pagenum": pagenum, "sortType": sortType }; 
} 
else if (pageName === "Search") { 
    scrollPath = "/SearchAjax/GetResultsKeyword/"; 
    sdata = { "pagenum": pagenum, "sortType": sortType, "keyword": keyword }; 
} 
else if (pageName === "Cat") { 
    scrollPath = "/SearchAjax/GetResultsCategory/"; 
    sdata = { "pagenum": pagenum, "sortType": sortType, "ID": categoryId, "Level": level }; 
} 
else if (pageName === "Merchant") { 
    scrollPath = "/SearchAjax/GetResultsMerchant/"; 
    sdata = { "pagenum": pagenum, "sortType": sortType, "ID": merchantId }; 
} 

}

和在頁面加載初始化函數:

function init(a, b, c, d, e, f, g) { 
getPageData(); 
eSc.ajax({ 
    type: 'POST', 
    url: scrollPath, 
    data: sdata, 
    success: function (data) { 
     eSc("#moreResults").html(data); 
    } 
}); 

}

用戶不明白的一個問題nd仍然會返回正確的數據,但每當有人在生產中從我們的網站加載更多數據時,我都會收到一封錯誤電子郵件(在開發過程中沒有發生,因此很難排除故障)。在檢查螢火蟲時,我看到正確的數據已通過。那麼,爲什麼我仍然得到這個錯誤?!

任何提示,爲什麼這可能會發生?

+3

如果您對使用'JSON.stringify'解決問題的原因感興趣:http:// encosia。 com/asmx-scriptservice-errors-invalid-json-primitive/ – 2012-08-16 20:17:09

回答

14
function init(a, b, c, d, e, f, g) { 
getPageData(); 
eSc.ajax({ 
    type: 'POST', 
    url: scrollPath, 
    contentType: 'application/json', 
    dataType: 'json', 
    data: JSON.stringify(sdata), 
    success: function (data) { 
     eSc("#moreResults").html(data); 
    } 
}); 

以json格式傳遞sData,使用JSON.stringify格式化json格式的數據。

它適用於我的情況。希望它適用於你的情況。

0

無論指定什麼Content-Type,jQuery都會使用URL編碼方案序列化$ .ajax()的數據參數。 我建議在AJAX使用內容類型:

function init(a, b, c, d, e, f, g) { 
getPageData(); 
eSc.ajax({ 
    type: 'POST', 
    url: scrollPath, 
    contentType: 'application/json', 
    dataType: 'json', 
    data: sdata, 
    success: function (data) { 
     eSc("#moreResults").html(data); 
    } 
}); 

而且你需要在數據參數使用引號。在您的版本中,它是JavaScript對象字面值而不是JSON字符串。

function getPageData() { 
pagenum = parseInt(eSc("#resultsBtn").attr("data-pagenum")); 
if (pageName === "Home") { 
    scrollPath = "/Home/GetResults/"; 
    sdata = '{ "pagenum":'+ pagenum +' , "sortType":'+ sortType +' }'; 
} 
else if (pageName === "Search") { 
    scrollPath = "/SearchAjax/GetResultsKeyword/"; 
    sdata = '{ "pagenum": ' + pagenum + ', "sortType": '+ sortType +', "keyword": ' + keyword +' }'; 
} 
else if (pageName === "Cat") { 
    scrollPath = "/SearchAjax/GetResultsCategory/"; 
    sdata = '{ "pagenum":'+ pagenum + ', "sortType":'+ sortType +', "ID":'+ categoryId +', "Level": '+level+' }'; 
} 
else if (pageName === "Merchant") { 
    scrollPath = "/SearchAjax/GetResultsMerchant/"; 
    sdata = '{ "pagenum":'+ pagenum +', "sortType":'+ sortType + ', "ID":'+ merchantId +'}'; 
} 

我希望它有幫助。

+0

很好,但是我收到以下錯誤(即使響應在螢火蟲中是正確的) – Ortal 2012-01-03 17:58:29

+0

抱歉,請輸入太快: 「great,howev呃,我收到以下錯誤(即使在螢火蟲中的響應是正確的)「SyntaxError:JSON.parse:意外字符」 我試着註釋掉contentTupe(我沒有收到json回來)並且有同樣的問題。 – Ortal 2012-01-03 18:04:05

+0

我編輯了代碼,因爲+符號丟失了。請現在嘗試一下。 – tildy 2012-01-04 10:30:09

1
  var param = "{'type': '" + type + "'}"; 
      var paramSfy = JSON.stringify({ type: type}) 
      var src = '/Physical_Inventory/Home/runZeroQtyDLIUpdate'; 
      $.ajax({ 
       type: "POST", 
       url: src, 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       data: paramSfy, 

我注意到的是,

如果你正在使用的contentType: 「應用/ JSON的;字符集= UTF-8」,那麼該數據有望爲一個字符串:

"{ "Param" : "Value" }" 

這最好通過使用JSON.stringify函數來完成。

如果不設置內容類型,則默認的是,「應用程序/ X WWW的窗體-urlencoded;字符集= UTF-8」如果使用此內容類型 ,則PARAM和值是嵌入式到數據可以像這樣在ajax中設置:

data: {Param : Value}, 
相關問題