我正在尝试将字符串发布到网络服务,但我收到此错误(Google Chrome扩展程序项目):
jquery-2.1.1.min.js:4 POST http://localhost:49242/Service.asmx/test 500(内部服务器错误)
这是我的ajax代码:
var data = {};
data.param1 = words[0];
$.ajax({
data: JSON.stringify({ 'data': data.param1 }),
dataType: 'application/json',
url: 'http://localhost:49242/Service.asmx/test',
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: function (result) {
alert(result);
},
failure: function (errMsg) {
alert(errMsg);
}
});
我的服务:
[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public string test(string param1) {
return param1;
}
我正在解决这个问题大约3天。你能帮助我吗 ?
顺便问一下,我有一个问题。我发布json变量与ajax服务(如你所见),但服务返回xml值。是否有问题或[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
此代码块解决问题?
答案 0 :(得分:4)
您的错误来自您的数据参数。字符串化UIScrollView
对象而不是data
:
{ 'data': data.param1 }
您的字符串化数据将生成var data = {};
data.param1 = words[0];
$.ajax({
data: JSON.stringify(data),
dataType: 'application/json',
url: 'http://localhost:49242/Service.asmx/test',
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: function (result) {
alert(result);
},
failure: function (errMsg) {
alert(errMsg);
}
});
,那么您的服务应该能够绑定{"param1":"Words"}
参数。
答案 1 :(得分:0)
我在AJAX帖子回复时遇到了这种类型的错误。我在这个问题上花了太多时间,最后我抓住了它。
它抛出500内部错误,因为AJAX响应有很多来自服务器的内容,所以它返回执行超时。
所以我刚刚在下面添加了一行,它工作正常。
Page.Server.ScriptTimeout = 300;