asmx页面,我调用Web服务,它返回一个JSON字符串,但我的jQuery帖子失败了,我不确定为什么?
$.post("/DesktopModules/EastlinkMilestones/MilestonesService.asmx /InsertYear_Decade",
{ Year: $('#txtYear_Decade').val(),
IsDecade: $('#chkIsDecade').prop('checked')
},
function (data)
{
Current_Edit_State == EDIT_STATE.UPDATE;
successMessage.text('Year/Decade created successfully.');
informationMessage.css('display', 'none');
successMessage.css('display', 'block');
},
"json"
)
.done(function(data) {
alert( "second success" );
alert( JSON.stringify(data));
})
.fail(function(jqXHR, textStatus, errorThrown) {
alert( "error" );
alert( JSON.stringify(jqXHR));
alert(textStatus);
alert(errorThrown);
})
.always(function() {
alert( "finished" );
});
.ASMX方法
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string InsertYear_Decade()
{ var Request = HttpContext.Current.Request;
var Response = HttpContext.Current.Response;
var js = new JavaScriptSerializer();
string mileStoneData = js.Serialize(data);
return mileStoneData; // {"ID": 20, "Year": "2013", "IsDecade": false }
}
}
Web方法总是失败,它的textStatus = parsererror和errorThrown =无效字符?
答案 0 :(得分:1)
您需要不返回值,只需写入响应即可。尝试这样的事情:
JavaScriptSerializer serializer = new JavaScriptSerializer();
Response.ContentType = "application/json";
Response.Write(serializer.Serialize(data));
答案 1 :(得分:-1)
您应该直接返回对象,而不是返回类型字符串。无需使用JavascriptSerializer,并在您的jquery帖子中将您的contentType设置为 application / json