• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
进步一点点
博客园    首页    新随笔    联系   管理    订阅  订阅
JQuery调用WCF服务,部署在iis

1.       配置服务端的web.confing文件图片中圈的地方需要着重配置

2.服务实现类需在类名上加上此语句(wcf服务可以在asp.net兼容模式为true或false的应用程序中运行)

  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

4.  接口中的方法上需加上{

[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

}语句(获取或设置服务操作或相应的http协议方法,包装请求)使用js或JQuery调用服务不可使用$.post()方法,

 如想使用请使用$.ajax()并将参数dataType属性值设置为jsonp,可以跨域访问,使用$.getJSON()方法调用服务时必须在类上加上

{[JavascriptCallbackBehavior(UrlParameterName = "名字自定义")]}在js的url后加上?UrlParameterName(自定义的名称)=?

 

5.  服务参数类型不限,返回值List集合、对象、字符等等测试都ok

6.服务接口与实现类

 1 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
 2     [ServiceContract]
 3     public interface IService1
 4     {
 5 
 6         [OperationContract]
 7         [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
 8         string Add(string name);
 9 
10         [OperationContract]
11         [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
12         List<Test> GetAllTest();
13         // TODO: 在此添加您的服务操作
14     }
 1  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
 2     //[JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]
 3     public class Service1 : IService1
 4     {
 5         public string Add(string name)
 6         {
 7             return "hello " + name;
 8         }
 9         public List<Test> GetAllTest()
10         {
11             List<Test> list = new List<Test>();
12             list.Add(new Test("赵海莹", 24, "女"));
13             list.Add(new Test("刘培华", 24, "男"));
14             return list;
15         }
16     }

7. JS调用方法

 1  $.ajax({
 2             url: "http://localhost:9014/Service1.svc/GetAllTest",
 3             type: "post",
 4             contentType: 'application/json',
 5             dataType: 'jsonp',
 6             success: function (returnValue) {
 7                 if (returnValue != null) {
 8                     for (var i = 0; i < returnValue.length; i++) {
 9                         $("#userlist").append("<li>姓名:" + returnValue[i].name + ",年龄:" + returnValue[i].age + ",性别:" + returnValue[i].sex + "</li>")
10                     }
11                 }
12             },
13             error: function () {
14                 alert("error")
15             }
16         });

 

 

 

 

 

1.       配置服务端的web.confing文件图片中圈的地方需要着重配置

 

posted on 2014-12-15 12:04  进步一点点  阅读(306)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3