Using JQuery to call a WebMethod

后端 未结 2 1776
我寻月下人不归
我寻月下人不归 2020-12-09 05:31

I am having trouble getting inside my Search WebMethod from my JQuery call. Maybe someone could help to point me in the right direction.

I also packed up everything

相关标签:
2条回答
  • 2020-12-09 06:10

    To solve a problem like this, the first thing to do is watch it in Firebug.

    If you click the "Search" link and watch the POST request/response in Firebug's console, you'll see it's throwing a 500 server error: Invalid JSON Primitive.

    The reason for that is because the key/value identifiers in your "data" JSON literal aren't quoted. Line 17 should be:

    data: "{'text':'" + search + "'}",
    

    Then, all will work as expected.

    Note: The suggested data { test: search } will not work. If you provide jQuery with an actual JSON literal instead of a string, it will convert that into a key/value pair of test=search and POST that instead of the JSON. That will also cause an ASP.NET AJAX ScriptService or PageMethod to throw the Invalid JSON Primitive error.

    0 讨论(0)
  • 2020-12-09 06:17

    You need to do the following (C#):

    • The WebMethod must be public static
    • It must be decorated with the [WebMethod] attribute
    • You need a ScriptManager on your .aspx page
    • Set the ScriptManager's EnablePageMethods="true"

    And here is some sample javascript:

    $().ready(function() {
        $(yourDropDownList).change(LoadValues);
    });
    
    
    function LoadValues() {
        PageMethods.YourMethod(arg1, CallSuccess, CallFailed);
    }
    
    function CallFailed(result) {
        alert('AJAX Error:' + result.get_message());
    }
    
    function CallSuccess(result) {
        //do whatever you need with the result
    }
    
    0 讨论(0)
提交回复
热议问题