简体   繁体   中英

Ajax call to webmethod gives ERROR 500 Internal server error

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM