[英]Ajax call to webmethod gives ERROR 500 Internal server error
I am trying to call a web method through ajax call. 我试图通过ajax调用来调用Web方法。
The jQuery code is : jQuery代码是:
$.ajax({
method: "POST",
url: "Login.aspx/LoginMethod",
data: { paramtr: "abc" },
contentType: "application/json; charset=utf-8",
dataType:'json',
success: function (result) {
swal("Done", "User added !", "success");
alert(result);
},
error: function () {
alert('0');
swal("Oops!", "Something went wrong!", "error")
}
});
and the web method code is: 网络方法代码为:
[System.Web.Services.WebMethod]
[ScriptMethod(UseHttpGet = false)]
public static string LoginMethod(string param)
{
string _param = param;
return "OKDONNE";
}
But I am getting Error 500 Internal server error and error function in ajax call gets called alerting '0'.Please help I have tried nearly everything! 但是我得到了错误500内部服务器错误和ajax调用中的错误功能被称为警报'0'。请帮助我尝试了几乎所有操作!
change this line data: { paramtr: "abc" },
to data: { param: "abc" },
. 将此行
data: { paramtr: "abc" },
更改为data: { param: "abc" },
data: { paramtr: "abc" },
Because your c# code accepts param
not paramtr
. 因为您的C#代码接受
param
而不是paramtr
。
I solved the issue by putting the following line of code : 我通过放置以下代码行解决了该问题:
data: JSON.stringify({ param: 1}),
Now everything is working fine without errors . 现在,一切正常,没有错误。 Thanks to @vivek for his inputs
感谢@vivek的投入
Always ensure your json key is the same as the parameters received by the web method. 始终确保您的json密钥与web方法接收的参数相同。 eg..
例如..
var obj = new Object();
obj.id = 'person';
obj.age = 30;
[WebMethod]
public static string savePerson(string id, int age){
string outcome = "";
return outcome;
}
I figured out after almost a day of error 500 经过近500天的错误后,我才知道
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.