jQuery的AJAX帖子:500(内部服务器错误)? [英] Jquery AJAX Post: 500 (Internal Server Error)?
问题描述
我试图向web服务发布一个字符串,但我得到这个错误(谷歌浏览器扩展项目):
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',
类型:'POST',
contentType:'application / json; charset = utf-8',
成功: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)]
这个代码块解决问题?
您的错误来自您的数据参数。将数据
对象替换为 {'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}
,那么你的服务应该能够绑定 param1
参数。
I am trying post a string to web service but I am getting this error (Google Chrome Extension Project):
jquery-2.1.1.min.js:4 POST http://localhost:49242/Service.asmx/test 500 (Internal Server Error)
Here is my ajax code:
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);
}
});
My service:
[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public string test(string param1) {
return param1;
}
I am working on this problem about 3 days. Can you help me ?
By the way, I have a question. I am posting json variable to service with ajax(like you see), but service returning xml value. Is there a problem or [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
this code block solving problem?
Your error come from your data parameter. Stringify data
object instead of { '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);
}
});
Your stringifyed data will result in {"param1":"Words"}
, then your service should be able to bind the param1
parameter.
这篇关于jQuery的AJAX帖子:500(内部服务器错误)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!