从javascript调用webmethod时,ASP.NET 500内部服务器出错

时间:2021-05-27 15:39:29

I'm trying to call the webmethod fucntionality using AJAX but unable to get the appropriate results. I have googled my problem and found many solution but those didn't worked for me. Please guide me what I'm doing wrong. Help will be appreciated.

我试图使用AJAX调用webmethod fucntionality但无法获得适当的结果。我搜索了我的问题并找到了许多解决方案,但那些对我没用。请指导我做错了什么。帮助将不胜感激。

Cheers

干杯

Code Snippet

代码片段

 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 in Behind 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."

4 个解决方案

#1


4  

First, it the webmethod is in the page class, and not in a Webservice class, then it should be static.

首先,webmethod是在页面类中,而不是在Webservice类中,那么它应该是静态的。

Second, the data transfered is not really a string, but an object, so change it to:

其次,传输的数据实际上不是字符串,而是一个对象,因此将其更改为:

var dataString = { 'value':  value  };

Third thing, "type" is for older versions of jquery, you should either change your ajax call to:

第三件事,“类型”是旧版本的jquery,您应该将ajax调用更改为:

method: "GET",
url: pagePath,
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",...

Or change the function in the server side to get post calls, by removing the

或者通过删除,更改服务器端的功能以获得发布呼叫

UseHttpGet = true

#2


3  

Probably you need to add static to your method declaration as below

您可能需要在方法声明中添加静态,如下所示

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string getUsername(string value)
{
   return "True";
}

if this isn't the case, you could F12 the browser->network then click on the error message to see it briefly.

如果不是这种情况,您可以使用F12浏览器 - >网络,然后单击错误消息进行简要查看。

Concerning the reported issue,the problem with get request, try to make it post

关于报告的问题,get请求的问题,尽量让它发布

#3


2  

The Answer is here :link

答案在这里:链接

the problem is with the annotation I was using the [ScriptMethod(UseHttpGet = true)] which causing the error. just change the value from true to false.

问题在于我使用的[ScriptMethod(UseHttpGet = true)]注释会导致错误。只需将值从true更改为false。

#4


0  

This is not specific to this question but you can check a few things to get better understanding of what is causing the issue.

这不是特定于这个问题,但你可以检查一些事情,以更好地了解导致问题的原因。

  • Check the status code and type of request in the General section of Request Headers.
  • 在“请求标头”的“常规”部分中检查请求的状态代码和类型。
  • Check the Response Headers if there is any json error.
  • 如果有任何json错误,请检查响应标头。
  • Check the Response to get the message, stacktrace and exception type.
  • 检查响应以获取消息,堆栈跟踪和异常类型。
  • Check the Request Payload section in Headers for parameters passed.
  • 检查Headers中的Request Payload部分是否已传递参数。

Check this post for more detail.

查看此帖子了解更多详情。

#1


4  

First, it the webmethod is in the page class, and not in a Webservice class, then it should be static.

首先,webmethod是在页面类中,而不是在Webservice类中,那么它应该是静态的。

Second, the data transfered is not really a string, but an object, so change it to:

其次,传输的数据实际上不是字符串,而是一个对象,因此将其更改为:

var dataString = { 'value':  value  };

Third thing, "type" is for older versions of jquery, you should either change your ajax call to:

第三件事,“类型”是旧版本的jquery,您应该将ajax调用更改为:

method: "GET",
url: pagePath,
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",...

Or change the function in the server side to get post calls, by removing the

或者通过删除,更改服务器端的功能以获得发布呼叫

UseHttpGet = true

#2


3  

Probably you need to add static to your method declaration as below

您可能需要在方法声明中添加静态,如下所示

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string getUsername(string value)
{
   return "True";
}

if this isn't the case, you could F12 the browser->network then click on the error message to see it briefly.

如果不是这种情况,您可以使用F12浏览器 - >网络,然后单击错误消息进行简要查看。

Concerning the reported issue,the problem with get request, try to make it post

关于报告的问题,get请求的问题,尽量让它发布

#3


2  

The Answer is here :link

答案在这里:链接

the problem is with the annotation I was using the [ScriptMethod(UseHttpGet = true)] which causing the error. just change the value from true to false.

问题在于我使用的[ScriptMethod(UseHttpGet = true)]注释会导致错误。只需将值从true更改为false。

#4


0  

This is not specific to this question but you can check a few things to get better understanding of what is causing the issue.

这不是特定于这个问题,但你可以检查一些事情,以更好地了解导致问题的原因。

  • Check the status code and type of request in the General section of Request Headers.
  • 在“请求标头”的“常规”部分中检查请求的状态代码和类型。
  • Check the Response Headers if there is any json error.
  • 如果有任何json错误,请检查响应标头。
  • Check the Response to get the message, stacktrace and exception type.
  • 检查响应以获取消息,堆栈跟踪和异常类型。
  • Check the Request Payload section in Headers for parameters passed.
  • 检查Headers中的Request Payload部分是否已传递参数。

Check this post for more detail.

查看此帖子了解更多详情。