【菜鸟学WCF】使用js+ajax调用WCF以及返回数据类型的控制
2014-05-09 09:16
1291 查看
先上代码,再谈问题。
Service1.svc.cs代码:
Service1.svc代码:
HTMLPage1.htm代码:
Web.config标记如下:
下面说说注意事项:
1、在Service1的DoWork定义上要用[WebGet]特性标记(默认是[WebInvoke]),因为在html页面中,我们通过xmlhttp.Open("GET".....)这样GET调用的。如果xmlhttp使用POST方法调用WCF服务,则DoWork需要用[WebInvoke]来标记;
2、在配置文件Web.config中,需要把endpointbehaviour节点下的behavior中的enableWebScript改成webHttp;
3、在html页面中,ajax调用的url为Service1.svc/myEndpoint/DoWork,其中myEndpoint为endpoint的地址,该地址在配置文件中赋值:
如果address=“”(赋值为空字符串),则url应该写成Service1.svc/DoWork。
示例程序,点击打开链接
如何控制请求和相应的数据格式呢,很简单,例如:
这样,调用DoWork就会返回json格式的数据了:
如果想返回xml格式的数据,只要更改ResponseFormat就OK了
Service1.svc.cs代码:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text; namespace WebApplication1 { [ServiceContract(Namespace = "GLM")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 { [OperationContract] [WebGet]//必须有这个特性,与Html页面中的ajax调用"GET"对应 public string DoWork() { return "DoWork called"; } } }
Service1.svc代码:
<%@ ServiceHost Language="C#" Debug="true" Service="WebApplication1.Service1" CodeBehind="Service1.svc.cs" %>
HTMLPage1.htm代码:
<!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 type="text/javascript"> function test() { var xmlhttp = CreateXmlHttpReq(); if (xmlhttp == null) return; var url = "Service1.svc/myEndpoint/DoWork"; xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && (xmlhttp.status == 200 || xmlhttp.status == 0)) { alert(xmlhttp.responseText); } } xmlhttp.open("GET", url, true); xmlhttp.send(null); } //创建XmlHttpRequest对象 function CreateXmlHttpReq() { var xmlhttp = null; try { xmlhttp = new XMLHttpRequest(); } catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Oops!Your Browser does not support Ajax"); } } } return xmlhttp; } </script> </head> <body> <input id="button1" type="button" value="click me" onclick="return test();" /> </body> </html>
Web.config标记如下:
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细消息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="WebApplication1.Service1AspNetAjaxBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <services> <service name="WebApplication1.Service1"> <endpoint address="myEndpoint" behaviorConfiguration="WebApplication1.Service1AspNetAjaxBehavior" binding="webHttpBinding" contract="WebApplication1.Service1" /> </service> </services> </system.serviceModel> </configuration>
下面说说注意事项:
1、在Service1的DoWork定义上要用[WebGet]特性标记(默认是[WebInvoke]),因为在html页面中,我们通过xmlhttp.Open("GET".....)这样GET调用的。如果xmlhttp使用POST方法调用WCF服务,则DoWork需要用[WebInvoke]来标记;
2、在配置文件Web.config中,需要把endpointbehaviour节点下的behavior中的enableWebScript改成webHttp;
<behaviors> <endpointBehaviors> <behavior name="WebApplication1.Service1AspNetAjaxBehavior"> <!--请看这里,原来是enableWebScript--> <webHttp/> </behavior> </endpointBehaviors> </behaviors>
3、在html页面中,ajax调用的url为Service1.svc/myEndpoint/DoWork,其中myEndpoint为endpoint的地址,该地址在配置文件中赋值:
<service name="WebApplication1.Service1"> <!--请看这里的address属性--> <endpoint address="myEndpoint" behaviorConfiguration="WebApplication1.Service1AspNetAjaxBehavior" binding="webHttpBinding" contract="WebApplication1.Service1" /> </service>
如果address=“”(赋值为空字符串),则url应该写成Service1.svc/DoWork。
示例程序,点击打开链接
如何控制请求和相应的数据格式呢,很简单,例如:
[OperationContract] [WebGet(ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)] public string DoWork() { return "DoWork called"; }
这样,调用DoWork就会返回json格式的数据了:
如果想返回xml格式的数据,只要更改ResponseFormat就OK了
相关文章推荐
- asp.net 使用ajax请求地址接口数据 ajax返回类型是json js get post请求数据
- 关于 ajax 动态返回数据 css 以及 js 失效问题
- 使用jquery、ajax不刷新页面打印表格(返回数据类型text、json);
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- JS脚本使用AJAX调用远程服务后获取返回的错误信息
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- [SoOnPerson]使用vue.js 来输出ajax返回的json数据
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(6):服务器端和客户端数据类型的自动转换:复杂类型
- Java调用C代码时传入以及返回基本数据类型
- ajax根据ID查询数据库并返回Json格式数据返回js,使用append显示到页面。判断json值为[]或者[[]]的问题。
- 【动态生成表格】ajax成功返回后,使用js,取到数据动态生成表格2017
- Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法
- asp.net mvc 使用Ajax调用Action 返回数据。
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 【菜鸟学WCF】使用ScriptManager+Ajax调用本地WCF服务
- Ajax 控件 的使用 以及js调用后台方法【自己总结一下】
- asp.net mvc 使用Ajax调用Action 返回数据。
- J2EE下使用AJAX(六) 后记以及如何处理复杂的返回值类型
- 使用ajax返回json类型数据的时候无法进入success可能的原因
- JSF与ajax使用h:messages 在commandButton上交互的问题以及在JSF前端使用ENUM数据类型