js调用WebService(复杂对象传入)[原创]
原创地址:http://longtianyu1.blog.163.com/blog/static/998196520111019315663/
实现原理,通过POST SOAP 1.2协议内容,实现复杂对象的传入
POST /Main.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<HelloWorld xmlns="http://tempuri.org/">
<user>
<UserID>int</UserID>
<UserName>string</UserName>
</user>
</HelloWorld>
</soap12:Body>
</soap12:Envelope>
WebService User.cs 传入对象
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
}
WebService Main.asmx.cs 服务
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class Main : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(User user)
{
return "Hello World" + user.UserName;
}
}
HTML页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="_Script/jquery-1.6.4.js" type="text/javascript"></script>
<script type="text/javascript">
function makeSOAP(content) {
return '<?xml version="1.0" encoding="utf-8"?>'
+ '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'
+ ' <soap12:Body>'
+ content
+ ' </soap12:Body>'
+ '</soap12:Envelope>';
}
$(function () {
$.ajax(
{ url: "/Main.asmx",
type: "POST",
contentType: "application/soap+xml; charset=utf-8",
data: makeSOAP('<HelloWorld xmlns="http://tempuri.org/"><user> <UserID>2</UserID> <UserName>string</UserName> </user> </HelloWorld>'),
success: function (xml) {
$("#info").text($(xml).find("HelloWorldResult").text())
}
});
});
</script>
</head>
<body>
<div id="info"></div>
</body>
</html>
js调用WebService(复杂对象传入)[原创]相关推荐
- JS调用WebService 无刷新获得在线消息
调用WebService传回的XML字符串: "<Messages><Message><ID>1</ID><Title>今天心情不 ...
- js 调用webservice接口
1:建立的webservice工程正确运行. a: 定义接口类 public interface IMyWebService { public String example(String messag ...
- Js 调用 WebService心得总结
页面: <INPUT type="button" value="Button" οnclick="getdata()">&l ...
- ajax调用第三方web服务,js调用soapWebService服务
js调用soapWebService服务 什么是 SOAP? SOAP 指简易对象访问协议 SOAP 是一种通信协议 SOAP 用于应用程序之间的通信 SOAP 是一种用于发送消息的格式 SOAP 被 ...
- PB调用WebService方式总结
文章目录 测试WebService接口 SOAP协议调用 HTTP方式调用SOAP 通过inet对象调用 通过XMLHTTP调用 HTTP方式直接调用 通过inet对象调用 通过XMLHTTP调用 P ...
- Javascript+xmlhttp调用Webservice
1. 创建webservice,为了免于落俗我稍稍修改了创建webserice的默认webmethod.^_^ using System; using System.Web; using Syste ...
- android调用web接口,Android调用webservice 接口
以查询手机号码归属地的Web service为例,它的wsdl为 1 在Android项目中导入??Ksoap2-android jar第三方jar包?? 2 Activity代码 public cl ...
- JS调用PageMethods(转)
操作步骤: 1.新建一个WebApplication项目,取默认设置. 2.双击设计界面,进入后天代码编辑界面,代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 学习Altas 笔记[js调用重载的方法出错,如何处理]
今天在使用js调用WebService的方法,发生了一个这样的错误"passed to method 'GetNewMessage' should not be a function&q ...
最新文章
- nodejs之http-proxy几点常见问题
- select中option改变时跳转到其他页面
- python十二:字符串格式化
- 5.Maven和Eclipse整合(两种方式进行插件的安装),Maven相关设置,Eclipse下创建Maven项目
- 六大技巧提升员工信息安全意识
- 对象的赋值和复制(转)
- LeetCode 1624. 两个相同字符之间的最长子字符串
- linux增量安装tomcat_Linux服务器上的tomcat中部署web项目
- java float存储方式_Java中小数的存储方式
- 设置eclipse主题风格
- Unity基础知识学习四,UI框架设计
- 搜狗输入法 rpm包_输入法哪个好用?2019年终横评来袭
- 第24期、宠物医院管理系统
- scrapy持久化存储
- linux 实验感悟_linux实验感想
- Java为什么不能写大型游戏?
- 高频课设-基于STM32的温度无线监测系统
- 手Q真的输给微信了吗?
- 怎么体现声屏障的美观性?
- css和html的用法,HTML与CSS之CSS的基本使用