0

我正在使用以下代码通过 jQuery ajax 调用 web 服务。但它不起作用?Web 服务以 JSON 格式返回值。如何通过此代码访问网络服务?

<html>
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script>
    $(document).ready(function () {
        $('input[id^="button"]').click(function () {
            alert('You have clicked ' + $(this).val());
            $.ajax({
                type: 'Get',
                url: 'http://localhost:56789/xxx/Handler.ashx?key=yyy ',
                success: function (data) {
                    alert(data);
                }
            });

        })
    })
    </script>

     <body>
        <div id="Sample_Div">
            <input type="button" id="button1" value="button1" />
        </div>
    </body>
</html>
4

2 回答 2

2

也许你可以试试这个。

 $.ajax({
        url: "../Services/Person.asmx/SavePersonById",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST",
        data: '{ID:"00123"}',
        success: function (response) {
            //do whatever your thingy..
    }
});

网络服务的东西:

[WebMethod]
public string SavePersonById(string ID)
    {
    //do some code here..
    dbContext.Save(ID,"Firstname","Lastnmae");
    return "Successfully Saved!";
    }
于 2013-04-16T08:37:45.167 回答
0

你可以试试这个:

$(document).ready(function () {
    $('#button').click(function () {
        $.ajax({
            type: "POST",
            url: "appWebservices/select.asmx/checkLogin",
            data: "{ ID:'" + $(this).val()+ "'}",
            contentType: "application/json;charset=utf-8",
            datatype: "json"
         });
     });
});

编写Web服务如下:

[WebMethod]
public string checkLogin(string ID)
{
    //Write your code here..
    //return value
}

了解更多详情

于 2016-12-09T12:08:08.547 回答