想测试下jQuery调用个第三方的WebService,利用 jQuery 的 $.get(url, data, callback) 调用了一个手机归属地查询的WebService。
PS: http://www.webxml.com.cn/zh_cn/index.aspx 网站里提供很多有用的简单的WebService。
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>jQuery Test</title>
- <mce:script src="http://code.jquery.com/jquery-1.4.3.js" mce_src="http://code.jquery.com/jquery-1.4.3.js" language="javascript"></mce:script>
- <mce:script type="text/javascript" language="JavaScript"><!--
- $(document).ready(function () {
- $("#btnGet").click(function () {
- var url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
- var mobile = $("#txtMobile").val();
- $.get(url, { mobileCode:mobile, userID:"" }, function (data) {
- var result = $(data).first().text();
- $("#divResponse").text(result);
- });
- });
- });
- // --></mce:script>
- </head>
- <body>
- <h1>jQuery 手机号归属地查询</h1>
- <div>
- MobileCode: <input id="txtMobile" type="text" style="width:100px" />
- <input type="button" id="btnGet" value="Get" style="width:80px" />
- </div>
- <hr />
- <div id="divResponse">
- </div>
- </body>
- </html>
这个服务(http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo)需要两个参数:mobileCode 和 userID,用 json 组装起来,调用 HTTP GET 就可以了。 程序很简单,自娱自乐而已。
运行结果如下:
![](http://hi.csdn.net/attachment/201103/9/0_1299689768TaLM.gif)