首页 >

jQuery实现ajax调用WCF服务办法介绍

web前端|js教程jQuery实现ajax调用WCF服务办法介绍
jQuery,ajax调用,WCF服务,jQuery实现ajax调用WCF服务
web前端-js教程关于AJAX调用WCF服务分为跨域和不跨域两种方式,今天咱们先介绍下不跨域下的调用方法。DEMO是在VS2008写的.
一物一码php源码,ubuntu里dns配置,一台tomcat加cdn,python爬虫需要环境,php开发pos机接口demo,seo8088lzw
经过测试与研究,发现AJAX调用WCF服务必须满足以下条件
laravel 商城项目源码,vscode 开发go项目,ubuntu 已用空间,tomcat怎么理解,sqlite3 建表,鼠标华东插件,前端图形化展示有什么框架,阿里网盘爬虫 猫影视,php array 元素,宁波seo优化营销,网站自动转载文章,网站下载的网页修改下面版权所有,bootstrap用户模板lzw
1.wcf的通讯方式必须使用webHttpBinding
2.必须设置节点的值
3.服务的实现必须添加标记
综合支付管理平台源码,vscode插件离线移植,ubuntu 卸载rvm,tomcat支持热部署,爬虫和龙,php静态调用属性,福清公司seo服务电话,食品招商加盟网站源码,如何找出模板隐藏的广告代码lzw
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

4.方法前面必须添加如下标记

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]

5.ajax方法中传递的参数名称必须和wcf服务中提供的参数方法名称一致

以下是本人写的代码,标记颜色的是需要注意的地方

服务器端配置文件代码

                                                                                                                         

服务器端代码

[ServiceContract]  public interface IService1  {   [OperationContract]   string GetData(int value);   [OperationContract]   City GetDataUsingDataContract(City composite);    [OperationContract]   List GetList();    [OperationContract]   List GetListData(List list);  }  // 使用下面示例中说明的数据约定将复合类型添加到服务操作。  [DataContract]  public class City  {   int seq = 0;   string cityID;   string ctiyName;    [DataMember]   public string CityID   {    get    {     return cityID;    }    set    {     cityID=value;    }   }   [DataMember]   public string CityName   {    get { return ctiyName; }    set { ctiyName = value; }   }   [DataMember]   public int Seq   {    get    { return seq; }    set    { seq = value; }   } }

实现代码

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  public class Service1 : IService1  {   [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]   public string GetData(int value)   {    return string.Format("You entered: {0}", value);   }   #region IService1 成员   [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]   public City GetDataUsingDataContract(City composite)   {    City c = new City();    c.CityID = composite.CityID;    c.CityName = composite.CityName;    c.Seq = composite.Seq;    return c;   }   [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]   public List GetList()   {    List list = new List();    City cc = new City();    cc.CityID = "1";    cc.CityName="北京";    cc.Seq = 3;    list.Add(cc);    City cc1 = new City();    cc1.CityID = "2";    cc1.CityName = "上海";    cc1.Seq = 4;    list.Add(cc1);    return list;   }   [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]   public List GetListData(List list)   {    return list;   }   #endregion }

客户端调用代码

           //参数为整数的方法   function fn1()   {    $.ajax({    url: "http://localhost:12079/Service1.svc/GetData",     type: "POST",     contentType: "text/json",     data: '{"value":2}',     dataType: "json",     success: function(returnValue) {      alert(returnValue);     },     error: function() {      alert('error');     }    });   } //参数为实体类的方法   function fn2() {    $.ajax({    url: "http://localhost:12079/Service1.svc/GetDataUsingDataContract",     type: "POST",     contentType: "application/json",     data: '{"CityID":1,"CityName":"北京","Seq":"3"}',     dataType: "json",     success: function(returnValue) {     alert(returnValue.CityID + ' ' + returnValue.CityName + "--" + returnValue.Seq);     },     error: function() {      alert('error');     }    });   } //返回值为类集合的方法   function fn3() {    $.ajax({     url: "http://localhost:12079/Service1.svc/GetList",     type: "POST",     contentType: "application/json",     dataType: "json",     success: function(returnValue) {     for (var i = 0; i < returnValue.length; i++) {      alert(returnValue[i].CityID + ' ' + returnValue[i].CityName+'---'+returnValue[i].Seq);      }     },     error: function() {      alert('error');     }    });   }   function fn4() {    $.ajax({    url: "http://localhost:12079/Service1.svc/GetListData",     type: "POST",     contentType: "application/json",     data: '[{"CityID":1,"CityName":"北京","Seq":"3"},{"CityID":3,"CityName":"上海","Seq":"3"}]',     dataType: "json",     success: function(returnValue) {     for (var i = 0; i < returnValue.length; i++) {      alert(returnValue[i].CityID + ' ' + returnValue[i].CityName + '---' + returnValue[i].Seq);     }     },     error: function() {      alert('error');     }    });   }        




jQuery实现ajax调用WCF服务办法介绍
  • jQuery+json实现的简易Ajax调用实例【jquery】
  • jQuery+json实现的简易Ajax调用实例【jquery】 | jQuery+json实现的简易Ajax调用实例【jquery】 ...

    jQuery实现ajax调用WCF服务办法介绍
  • jQuery Ajax调用WCF服务详细教程【jquery】
  • jQuery Ajax调用WCF服务详细教程【jquery】 | jQuery Ajax调用WCF服务详细教程【jquery】 ...

    jQuery实现ajax调用WCF服务办法介绍
  • jQuery实现ajax调用WCF服务办法(附带demo下载)【jquery】
  • jQuery实现ajax调用WCF服务办法(附带demo下载)【jquery】 | jQuery实现ajax调用WCF服务办法(附带demo下载)【jquery】 ...