在ASP.NET Web应用程序中,我有一个WebMethod
。我从jQuery AJAX post请求调用这个web方法。我想将结果作为json对象返回。
我的网络方法:
[WebMethod]
public static string OnSubmit(string Email, string Message)
{
EmailHelper.sendSupportEmail(Email, Message);
string message = "Thank you for your submission, we will be looking into it as soon as possible.";
int status = 200;
JavaScriptSerializer serializer = new JavaScriptSerializer();
ReturnResult result = new ReturnResult() { Status = status, Message = message };
return serializer.Serialize(result);
}
public class ReturnResult
{
public int Status {get;set;}
public string Message{get;set;}
}
基本上我只是调用sendSupportEmail
函数,然后使用JavascriptSerializer将其序列化为Json并返回它。
客户端我有
$.ajax({
type: "POST",
url: "Contact.aspx/OnSubmit",
data: data,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Oops, we are unable to process your request at this time. Please try again later.");
},
success: function (result) {
var obj = jQuery.parseJSON(result);
var status = obj.Status;
var message = obj.Message;
}
});
请求成功但我在jQuery.parseJSON(result);
JavaScript运行时错误:字符无效
我尝试自己编写json字符串,但仍然无效。
由于
答案 0 :(得分:1)
问题在于我必须使用result.d
所以代码是
var obj = jQuery.parseJSON(result.d);