ASP.NET 500 Internal Server Error while calling webmethod from javascript

前端 未结 5 2056
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-02 17:53

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 thos

相关标签:
5条回答
  • 2021-01-02 18:16

    In my case the problem was the "data" field (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, then 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
    
    0 讨论(0)
  • 2021-01-02 18:24

    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.
    • Check the Response to get the message, stacktrace and exception type.
    • Check the Request Payload section in Headers for parameters passed.

    Check this post for more detail.

    0 讨论(0)
  • 2021-01-02 18:27

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

    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:

    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
    
    0 讨论(0)
  • 2021-01-02 18:35

    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.

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

    0 讨论(0)
  • 2021-01-02 18:35

    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.

    0 讨论(0)
提交回复
热议问题