1
這是我的代碼。我遇到的問題是代碼jsonVersionList[0]
得到了一個字符串「[」,實際上我想解析爲jsonVersionList
作爲Json對象,我認爲原因是JavaScript把jsonVersionList
當作字符串而不是Json對象。請幫助我。謝謝。ApiController返回的Json結果無法解析,爲什麼dataType:「json」不起作用?
function ajaxGetSystemTraceLog(jsonRequest) {
var sUrl = "/api/SysTraceLog";
$.ajax({
cache: false,
type: "POST",
async: false,
url: sUrl,
data: jsonRequest,
contentType: "application/json",
dataType: "json",
success: function (result) {
var sHtml = buildLogDiv(JSON.stringify(eval(result)));
$("#effect").html(sHtml);
showCenter("#effect", false, 0, 0);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}
function buildLogDiv(jsonVersionList) {
var sHtml = "<table style=\"width:100%;\"><tr><td style=\"width:100%;background-color:#005CE6;\" align=\"right\"><img onclick=\"hide('#effect', false);\" alt=\"close it\" title=\"close it\" src=\"/content/themes/default/images/cancel.png\" width=\"20\" height=\"20\" /></td></tr>";
sHtml += "<tr><td style='text-align:left'>";
sHtml += jsonVersionList[0];
sHtml += "</td></tr></table>";
return sHtml;
}
public class SysTraceLogController : ApiController
{
public string Post(QueryTraceLogRequestModel queryTraceLogReq)
{
return "[\"1\",\"2\",\"3\",\"4\"]";
}
}
這就是我在更改爲'JSON.stringify(eval(result))'之前所做的,它不起作用。謝謝。 –
@ Joe.wang由於某種原因,jQuery不解析數據,請嘗試'var sHtml = buildLogDiv(JSON.parse(result)); ' – Musa
OMG,它的工作原理,簡單而簡單...謝謝。 –