开发者

Calling Web Service from Javascript - what's it doing under the hood?

Recently a colleague had to call a .NET 2.0 web service from script. We noticed that we'd have to put the [ScriptServi开发者_JS百科ce] attribute, either via AJAX extensions or upgrading to 3.5.

That's all I've been able to find out - no-one explains what it's doing under the hood!

Can anyone enlighten me?

Thanks Duncan


Strictly speaking, you don't need to do anything to a web service in order to make it callable from script.

See here: How to call web service using vbscript (synchronous)?

A web service is just a system that is addressible via remote protocols, usually web-based protocols. Often that means HTTP. A simple request can just be performed via an HTTP GET on a particular URL - the segments in the URL path or the query string make up the "parameters" to the web service request.

A client might send an HTTP GET to

http://server/appPath/p1/p2/p3

And the application listening there would be responsible for unpacking that URL, maybe mapping the p1, p2, p3 into query parameters, and then determining how to respond to it.

For more complex requests, the transaction might be HTTP POST, and the format of the payload is either XML, JSON, or something else that your application specifies.

In this case the URL may be like so:

http://server/appPath/resource1

and the POST'd payload, if using JSON, might be:

{"Age":35,"FirstName":"Peyton","LastName":"Manning"} 

if using XML, it might be:

<person>
  <Age>35</Age>
  <FirstName>Peyton</FirstName>
  <LastName>Manning</LastName>
</person>

You can form a request that complies to those constraints in any programming language, including script like Javascript.

If you are using SOAP, then that XML document needs to be wrapped in a SOAP envelope. See Calling WCF service by VBScript for an example (VBScript, but easily translatable to Javascript).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜