ASP.NET 500 internal server error when calling webmethod from javascript
I am trying to call fucntionality webmethod
using AJAX but cannot get the appropriate results. I ran into the problem and found many solutions, but it didn't work for me. Please tell me what I am doing wrong. Help would be appreciated.
Greetings
Snippet of code
function checkUserNameExists() {
//initialization
var pagePath = window.location.pathname + "/getUsername";
var value = document.getElementById('control_userName').value;
var dataString = "{ 'value':'" + value + "' }";
$.ajax({
type: "GET",
url: pagePath,
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",
error:
function (XMLHttpRequest, textStatus, errorThrown) {
},
success:
function (result) {
var flag = true;
if (result != null) {
flag = result.d;
if (flag == "True") {
alert('okay fine you are good');
}
else {
alert('try again');
}
}
}
});
}
Method by code file
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string getUsername(string value)
{
return "True";
}
Exception
ExceptionType: "System.InvalidOperationException"
Message: "An attempt was made to call the method 'getUsername' using a POST request, which is not allowed."
source to share
First, it is a web method in the page class, not in the Webservice class, then it should be static.
Secondly, the passed data is not actually a string, but an object, so change it to:
var dataString = { 'value': value };
Third, "type" is for older versions of jquery, you should either change your ajax call to:
method: "GET",
url: pagePath,
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",...
Or change the function on the server side to get post calls by removing
UseHttpGet = true
source to share
You probably need to add static to the method declaration below:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string getUsername(string value)
{
return "True";
}
If it is not, you can F12 on the web browser → then click on the error message to see it briefly.
Regarding the reported issue, Pull Request issue, try this post
source to share
This is not relevant to this question, but there are several things you can check to get a better understanding of the cause of the problem.
- Check the status code and request type in the general section of request headers .
- Check response headers if there is any json error.
- Check the Answer to get the message, stack trace and type of exception.
- Check the Request Payload section in the headers for passed parameters.
Check out this post for more details.
source to share
In my case, the problem was the data field (in both cases, GET and POST). As a test, remove the "data" from the AJAX call, and also remove the web method parameter, if it works, the problem is the format of the "data" field:
$.ajax({
type: "GET",
url: pagePath,
contentType: "application/json; charset=utf-8",
dataType: "json",
...
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string getUsername()
{
return "True";
}
Some examples:
data: JSON.stringify({ "parameter": variable }) WORKS
data: JSON.stringify({ parameter: variable }) WORKS
data: '{"parameter": "' + variable + '"}' WORKS
data: '{parameter: ' + variable + '}' don't works
data: JSON.stringify({ 'parameter': 'value' }) WORKS
data: '{"parameter":"value"}' WORKS
data: "{'parameter':'value'}" WORKS
data: '{parameter:value}' don't works
data: {parameter:value} don't works
data: {"parameter":"value"} don't works
data: {'parameter':'value'} don't works
source to share