psyborg.eth
psyborg.eth

Reputation: 320

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!

Upvotes: 2

Views: 10429

Answers (3)

Gods1son
Gods1son

Reputation: 21

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 outcome = "";

    return outcome;
}

I figured out after almost a day of error 500

Upvotes: 2

psyborg.eth
psyborg.eth

Reputation: 320

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

Upvotes: 2

vivek
vivek

Reputation: 1605

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

Because your c# code accepts param not paramtr.

Upvotes: 3

Related Questions