用情
记录我的程序人生
随笔 - 2,  文章 - 0,  评论 - 1,  阅读 - 3288

This is my first blog in English, maybe a little weird, but I will write all my blogs in English from now on. No for special purposes or something else, just to practice my English and write down my thinking and idea during the work.

 

Background:

We have already had a Web service, and now I need to access the web service on java script. Yes, most of you know what is web service and SOAP and WSDL etc., but before the beginning this article, I would like to introduce these basic concepts:

1. Web service: A method of communication between two electronic devices( computers ) over a network. If you are interested in the detail, see here.

2. SOAP: Simple Object Access Protocol, it defines how web service works. You have to remember two things: XML and HTTP. see here.

3. WSDL: Web Service Description Language. I consider it as what a web service provides. see here.

How to consume a web service:

All right, we know what are web service and SOAP and WSDL, but how do they work? Firstly, we must have a web service, I create a web service in Asp.net, so easy, isn’t it? You will see 4 marks, what are they ? They’re the C# syntax to create a web service, but has nothing to do with the standard web service protocol.

image

1. WebService Attribute indicates this class is a web service.

2. ScriptService Attribute, we will discuss it in another article.

3. WebMethod attribute indicate this method should be treated as a web method.

4. The web method with a parameter and a return value, to demonstrate how to consume a web method with parameter.

Now we can view this web service in browser, you will see the brief of the web service.

 

image

1. WebService1 is the web service we have created.

2. Service Description is the WSDL of this web service, if you click the link you will see the detail of the the description.

3. Web methods we defined.

 

We have defined a web service, but how to consume it? Click the Hello method, the web URL changes to “http://localhost:14828/WebService1.asmx?op=Hello”, we will see the detail definition of the web service method:

image

The first block is the SOAP definition of the request while the second block is the response definition.

1. The request http post header.

2. The request body.

3. The input parameter userName, replace the “string” placeholder to the concrete user name.

4. The response header.

5. the response body.

6. The response result, read this value to get the result.

 

According to the SOAP, we know how to request and response the Hello method. JQuery provides an easy way to do this:

 

function Hello(userName) {
        
         var data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\
                    <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\
                      <soap:Body>\
                        <Hello xmlns=\"http://tempuri.org/\">\
                          <userName>" + userName + "</userName>\
                        </Hello>\
                      </soap:Body>\
                    </soap:Envelope>";

         $.ajax("http://localhost:14828/WebService1.asmx?op=Hello",
               { type: "POST",
                   data: data,
                   contentType: "text/xml; charset=utf-8",
                   success: successCallback
               });       
     }

    function successCallback(response, status, xhr)
    {
        $("#txtResponse").attr("value", response.xml);        
    }

As you can see, the Hello method post the SOAP body to server and the server responses the SOAP response body to client. In client, we can use JQuery to parse the response xml and get the data we want.

Summary:

This is a simple demo to use JQuery to consume a web service via SOAP, there are still some issues we should consider while consuming a Web service via java script, such as cross domain etc. I think if we understand the basic concepts clearly, we can make things easier.

Hope this helpful.

posted on   用情  阅读(3247)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

< 2011年10月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示