【问题标题】:Calling .NET Web Service from jQuery从 jQuery 调用 .NET Web 服务
【发布时间】:2011-10-10 20:51:45
【问题描述】:

我从一个不属于项目的 jQuery 文件调用 .NET Web 服务。每当我调用该服务时,它都会显示 OPTIONS /HOCWebService.asmx/HelloWorld 并且不返回任何内容。到底是怎么回事?在 web.config 中,我指定了允许使用 httpGet 和 httpPost 的 Web 服务。

更新1:

$.ajax(

    {
        type: "POST",
        url: "http://127.0.0.1:8080/HOCWebService.asmx/HelloWorld",
        data: "{}",
        dataType: "json",
        contentType: "application/json",
        success: function (response) {

            alert(response.d); 

            var categories = $.evalJSON(response.d);


            for (i = 0; i < categories.length; i++) {

                var span = $(document.createElement("span"));
                $(span).addClass("ui-li-count");
                $(span).html(categories[i].Count);
                var li = $(document.createElement("li"));
                var anchor = $(document.createElement("a"));
                $(anchor).attr("href", "/Home/detail/"+categories[i].Id);
                $(anchor).html(categories[i].Title);

                $(li).append(anchor);
                $(li).append(span);

                //     $("#categoriesListView").append('<li><a href="/Home/detail/' + categories[i].Id + '">' + categories[i].Title + '</a></li>');

                $("#categoriesListView").append(li);

                //  $(span).text(categories[i].Count);

            }

            $("#categoriesListView").listview('refresh');

        }
    }

    );

【问题讨论】:

  • 你可以为 $.ajax() 调用添加代码吗?
  • @StevendeSalas 代码已添加!

标签: web-services


【解决方案1】:

.NET 框架中 ASMX 文件的默认实现意味着您正在处理 SOAP Web 服务,因此将发送和接收 XML wrapped in a SOAP envelope(而不是 JSON)。

试试:

$.ajax({
          // 1. Loose the 'HelloWorld' from the URL
          url: "http://127.0.0.1:8080/HOCWebService.asmx", 
          type: 'POST',
          async: false,
          dataType: 'xml',
          // 2. But add it as a HTTP Header called 'SOAPAction'
          headers: {
             SOAPAction: "http://www.tempuri.org/HelloWorld"
          },
          contentType: 'text/xml; charset="utf-8"',
          // 3. The data sent to the server must be a SOAP XML Envelope
          data: '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
                   '<soap:Body>' +
                       '<HelloWorld xmlns="http://www.tempuri.org/" />' +
                    '</soap:Body>' +
                '</soap:Envelope>',
          sucess: function(response) {
               alert(response.responseText);
               // Completion logic goes here
          }
    });

请注意,作为上述实现的一部分,您需要一个名为“SOAPAction”的 HTTP POST 标头与您正在调用的方法相匹配,否则它将不起作用:

 headers: {
    SOAPAction: "http://www.tempuri.org/HelloWorld"
 },

表示 POST 请求将包含以下最后一行:

POST /HOCWebService.asmx HTTP/1.1
Host: 127.0.0.1:8080
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 453
SOAPAction: "http://www.tempuri.org/HelloWorld"

http://www.tempuri.org/ 是 Microsoft 在您创建新的 ASMX 服务时使用的默认命名空间,请随时将其更新为您在实施中使用的实际命名空间。

建议:

如果您需要从您的应用程序中向后和向前发送 JSON,我可以建议您使用通用处理程序(ASHX 文件)并使用 something similar to this approach

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2010-10-19
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 2011-12-24
    相关资源
    最近更新 更多