Call server side function using $.ajax

前端 未结 2 2035
名媛妹妹
名媛妹妹 2021-01-23 07:00

Ultimately, I\'d like to send a value to the server on a button click and query my DB. For now, I\'m having trouble using jquery.ajax to call a function on the server side. He

相关标签:
2条回答
  • 2021-01-23 07:25

    There are a couple of things which are not quite correct:

    • You should put your data inside quotes
    • and the name of the member should match the name of the web method's parameter
    • the value for sendData should be in double quotes
    • you should add contentType and dataType

    Full example:

    function send() {
        $.ajax({
            type: "POST",
            url: "ajax.aspx/Test",
            data: '{ sendData: "ok" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) { alert("successful!" + result.d); }
        });
    }
    

    This code works for me.

    0 讨论(0)
  • 2021-01-23 07:27

    http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ this link could be usefull

    EDIT: your ajax call have to look like this:

     $.ajax(
                {
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "ajax.aspx/Test",
                    data: "{ sendData: 'ok' }",
                    success: function (result) { alert("successful!"); }
                })
    
    0 讨论(0)
提交回复
热议问题