响应javascript ajax与对象解析失败

时间:2015-04-07 13:23:52

标签: javascript c# ajax

我在哪里错了!?

Javascript代码:

var url = "/SitePages/AlertsHandler.aspx/GetAlert";
$.ajax({
    type: "POST",
    url: url,
    data: '{alertId: \"' + alertId + '\"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        console.log(data.IncidentDesc);
    }
 });

C#代码:

[WebMethod]
    public static string GetAlert(string alertId)
    {
        return MyJsonObject; // on debug --> {"IncidentDesc":"assdafsdaf","IncidentRecommend":"asdfsdaf"}
    }

我在控制台上得到了不确定...我在这里想念的是什么?

更新,发现问题:

var data2 = JSON.parse(data.d);
console.log('IncidentDesc:' + data2.IncidentDesc);

2 个答案:

答案 0 :(得分:0)

try this

var url = "/SitePages/AlertsHandler.aspx/GetAlert";
var data={alertId: alertId};
$.ajax({
    type: "POST",
    url: url,
    data: JSON.stringify(data),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        console.log(data.IncidentDesc);
    }
 });

答案 1 :(得分:-1)

您可能需要解析JSON数据,因为它可能是响应时的字符串?

var obj = JSON.parse(data);

console.log(data.IncidentDesc);