简体   繁体   English

asp.net网络服务(.asmx)

[英]asp.net webservices (.asmx)

Internally webservices use soap to work over HTTP. 内部Web服务使用soap通过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? 但是,当我们尝试访问Web服务的[WebMethod]时,如何基于带有jquery ajax的URL来开始工作? Does SOAP still playing the role with jQuery ajax? SOAP是否仍然在jQuery ajax中发挥作用? If yes how? 如果是,怎么办? If not why not? 如果不是,为什么不呢? You can use below example to keep thing simple. 您可以使用下面的示例使事情保持简单。

Below is the code from asmx: 以下是来自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";
    }
}

It is possible to call WebMethods with AJAX as the transport is HTTP . 由于传输是HTTP ,因此可以使用AJAX调用WebMethods You can find many examples of it in the internet and on SO: 您可以在Internet和SO上找到许多示例:

jQuery AJAX call to an ASP.NET WebMethod jQuery AJAX调用ASP.NET WebMethod

Calling ASP.Net WebMethod using jQuery AJAX 使用jQuery AJAX调用ASP.Net WebMethod

SOAP is an envelope for the payload (with some additional features). SOAP是有效负载的信封(具有一些其他功能)。 It is up to you whether you want to use it in WebMethod or not. 是否要在WebMethod使用它取决于您。

Here is how you create a Hello World service in web application project: 这是在Web应用程序项目中创建Hello World服务的方式:

[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: 这是使用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". 来自服务器的响应将为{d: "Hello World"}因为jQuery将添加Accept标头“ 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"}

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

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