admin管理员组

文章数量:1296485

I am trying to call a web method through ajax call.

The jQuery code is :

$.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!

I am trying to call a web method through ajax call.

The jQuery code is :

$.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!

Share Improve this question asked Jul 21, 2016 at 8:37 psyborg.ethpsyborg.eth 3201 gold badge5 silver badges18 bronze badges 2
  • Is asp able to build a parameter list? I think, LoginMethod can't have any arguments. You have to get the parameters elsewhere. – Holger Commented Jul 21, 2016 at 9:43
  • LoginMethod can take parameters . – psyborg.eth Commented Jul 21, 2016 at 9:49
Add a ment  | 

3 Answers 3

Reset to default 3

change this line data: { paramtr: "abc" }, to data: { param: "abc" },.

Because your c# code accepts param not 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

Always ensure your json key is the same as the parameters received by the web method. e.g...

var obj = new Object();
obj.id = 'person';
obj.age = 30;

[WebMethod]
public static string savePerson(string id, int age){
    string oute = "";

    return oute;
}

I figured out after almost a day of error 500

本文标签: javascriptAjax call to webmethod gives ERROR 500 Internal server errorStack Overflow