net平台webservice跨域调用以及node调用webservice
2015-08-30 18:26
701 查看
跨域调用
net平台webservice默认的输出是xml结构,如果在客户端js设定了json数据结构,3.5以上平台会自动转为json格式,但是数据会被放置在一个d的属性中(有点莫名其妙,有知道为什么这样的同学请告知)。而且不支持跨域调用,如果强行设定“Access-Control-Allow-Origin”消息头不起效果。服务端代码:
[WebMethod] public void 方法名(参数) { this.Context.Response.AddHeader("Access-Control-Allow-Origin", "*"); //设定跨域头 string callback = HttpContext.Current.Request["jsoncallback"];//此处需要和客户端js代码对应 JavaScriptSerializer jss = new JavaScriptSerializer(); string jsonObjStr = jss.Serialize(结果对象); HttpContext.Current.Response.Write(callback + "(" + jsonObjStr + ")"); HttpContext.Current.Response.End(); }
客户端js代码:
$.ajax({ url: "http://域名/服务名.asmx/方法名", data: { 参数1:参数值1,..... }, type: 'post', cache: false, contentType: "application/json; charset=utf-8", dataType: 'jsonp', jsonp: 'jsoncallback',//和服务端对应,如果没有此函数,则默认执行success的函数 success: function (data) { var r = data; //此处data为json结构的服务端返回数据结果,而非执行代码 }, error: function (XMLHttpRequest, textStatus, errorThrown) { //alert(XMLHttpRequest.status); //alert(XMLHttpRequest.readyState); //alert(textStatus); }, complete: function (XMLHttpRequest, textStatus) { } });
node.js服务端调用net的webservice
需要先安装soap组件,注意要安装在程序安装目录下:npm install soap
node.js的js代码:
需要注意要调用的是webservice服务的WSDL描述文档地址var soap = require('soap'); soap.createClient("http://域名/服务名.asmx?WSDL", function (err, client) { client.调用方法名({ 参数名1: 参数值1,.... }, function (err, result) { //返回结果结构需要根据WSDL中描述使用 }); });还需要注意的是,node.js对服务的调用保持了一贯异步性,并非如C#一样线性。
对于net的webservice无特殊要求,常规即可,无需进行跨域处理。
相关文章推荐
- Count Complete Tree Nodes
- Swap Nodes in Pairs
- LeetCode-Count Complete Tree Nodes
- nodejs处理get请求
- 一步一步讲解安装NodeJs开发环境
- Nodejs初阶之express
- leetcode_019Remove Nth Node From End of List
- btrfs连载(一)inode.c之btrfs_create函数
- LeetCode Swap Nodes in Pairs
- LeetCode Swap Nodes in Pairs
- 2015-08-04个人定制(nodejs中的循环陷阱)
- HDFS架构——NameNode
- 25 Reverse Nodes in k-Group
- Leetcode 222: Count Complete Tree Nodes
- Delete Node in a Linked List 单链表删除节点
- NodeJS 读取XML文件
- LeetCode2.2.9(Reverse Nodes in k-Group)
- LeetCode2.2.8(Swap Nodes in Pairs)
- LeetCode2.2.7(Remove Nth Node From End of List)
- 在Ubuntu上安装Node.js的Upstream版本