简体   繁体   English

从jQuery Ajax远程调用ASP.Net Web服务

[英]Remote call ASP.Net Web Service from jQuery Ajax

I have the following problem, there is one ASP. 我有以下问题,有一个ASP。 NET Web Service project (*. asmx), with several function and is the second project, also ASP .NET, which should call a service using jQuery Ajax (no service reference). NET Web服务项目(*。asmx),具有多个功能,并且是第二个项目,也是ASP .NET,应使用jQuery Ajax调用服务(无服务引用)。 Web Service project: Web服务项目:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class MathService : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod]
    public int Add(int x, int y)
    {
        return x + y;
    }
}

and

<script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "http://localhost:47204/Services/MathService.asmx/Add",
            data: "{'x': '5', 'y': '10'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (res) {
                $("#divMathService").text(res.d);
            },
            error: function () {
                alert("ALARM!");
            }
        });
    });
</script>

This - localhost:47204 - host web service project, when i debug project. 当我调试项目时,这-localhost:47204-托管Web服务项目。 I specifically pointed out the full URL, something to use it in the client project. 我特别指出了完整的URL,这是在客户端项目中要使用的内容。 Project works, everything all right. 项目正常,一切都很好。 The project client looks as follows: 项目客户端如下所示:

<script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
       $.ajax({
            type: "POST",
            url: "http://localhost:47204/Services/MathService.asmx/Add",
            data: "{'x': '5', 'y': '10'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (res) {
                $("#divMathService").text(res.d);
            },
            error: function () {
                alert("ALARM!");
            }
        });
    });
</script>

But this project don't show any result, only alert, I don't understand why. 但是这个项目没有显示任何结果,只是警告,我不明白为什么。 Maybe problem with permission for remote call? 也许远程通话许可有问题?

I can't attach project, but can send to email if someone wants to look at the project. 我无法附加项目,但是如果有人想查看该项目,可以发送电子邮件。

try formatting your data as valid json first: 尝试先将数据格式化为有效json:

'{"x": 5, "y": 10}'

also, your method needs to know to send the response as json: 另外,您的方法需要知道将响应作为json发送:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public int Add(int x, int y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM