Call server side function using $.ajax

前端 未结 2 2036
名媛妹妹
名媛妹妹 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.

提交回复
热议问题