7*4 2017-04-19 10:55 采纳率: 50%
浏览 45

asp.net网络服务(.asmx)

Internally webservices use soap to work over HTTP. But when we try to access a [WebMethod] of a web service, how things start working on the basis of URL with jquery ajax? Does SOAP still playing the role with jQuery ajax? If yes how? If not why not? You can use below example to keep thing simple.

Below is the code from asmx:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class MyService : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {          
        return "Hello World";
    }
}
  • 写回答

1条回答 默认 最新

  • 妄徒之命 2017-04-19 12:56
    关注

    It is possible to call WebMethods with AJAX as the transport is HTTP. You can find many examples of it in the internet and on SO:

    jQuery AJAX call to an ASP.NET WebMethod

    Calling ASP.Net WebMethod using jQuery AJAX

    SOAP is an envelope for the payload (with some additional features). It is up to you whether you want to use it in WebMethod or not.

    Here is how you create a Hello World service in web application project:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
    

    Here is how you can consume it with jQuery:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
    
    <script>
        console.log($.ajax);
        $.ajax({
            type: "POST",
            url: "http://localhost:55501/WebService1.asmx/HelloWorld",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response.d) {
                alert(response.d);
            }
        });
    </script>
    

    And response from server will be {d: "Hello World"} because jQuery will add Accept header "application/json".

    Here is how you can consume it from console app:

    static void Main(string[] args)
    {
        var client = new HttpClient();
        var uri = new Uri("http://localhost:55501/WebService1.asmx/HelloWorld")
    
        // Get xml
        var response = client.PostAsync(uri, new StringContent("")).Result;
        Console.WriteLine(response.Content.ReadAsStringAsync().Result);
    
        Console.WriteLine();
    
        // Get Json
        var response1 = client.PostAsync(uri,
            new StringContent("", Encoding.UTF8, "application/json")).Result;
        Console.WriteLine(response1.Content.ReadAsStringAsync().Result);
    }
    

    Which will output:

    <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://tempuri.org/">Hello World</string>
    
    {"d":"Hello World"}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)