码迷,mamicode.com
首页 > 其他好文 > 详细

WCF跨域调用

时间:2015-11-06 10:54:11      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

1  WCf跨域调用  一个用 Jsonp  (这是蒋金楠大神写的一个实例这里把他拷贝过来)

在契约中

    [ServiceContract]
    public interface IEmployees
    {
        [WebGet(UriTemplate = "all",ResponseFormat =WebMessageFormat.Json)]      
        IEnumerable<Employee> GetAll();
    }
    public class Employee
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Department { get; set; }
        public string Grade { get; set; }
    }



2 在实现类中


   public class EmployeesService : IEmployees
    {
        public IEnumerable<Employee> GetAll()
        {
            return new List<Employee>
            {
                new Employee{ Id = "001", Name="张三", Department="开发部", Grade = "G6"},    
                new Employee{ Id = "002", Name="李四", Department="人事部", Grade = "G7"}, 
                new Employee{ Id = "003", Name="王五", Department="销售部", Grade = "G8"}
            };
        }
    }


3 配置
  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint crossDomainScriptAccessEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
    <bindings>
      <webHttpBinding>
        <binding crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>
    <services>      
      <service name="Artech.WcfServices.Service.EmployeesService">
        <endpoint kind="webHttpEndpoint" 
                  address="http://127.0.0.1:3721/employees"
                  contract="Artech.WcfServices.Service.Interface.IEmployees"/>
      </service>
    </services>
  </system.serviceModel>


4 在前台调用的时候
  <script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: "get",
                url: "http://127.0.0.1:3721/employees/all",
                dataType: "jsonp",
                success: function (employees) {
                    $.each(employees, function (index, value) {
                        var detailUrl = "detail.html?id=" + value.Id;
                        var html = "<tr><td>";
                        html += value.Id + "</td><td>";
                        html += "<a href=‘" + detailUrl + "‘>" + value.Name + "</a></td><td>";
                        html += value.Grade + "</td><td>";
                        html += value.Department + "</td></tr>";
                        $("#employees").append(html);
                    });
                    $("#employees tr:odd").addClass("oddRow");
                }
            });

        });
    </script>

 2  第二张 就是正成的网页异步调用只需要在网页中的后台添加2个指令

 

 public ActionResult GetTest()
        {
            this.Response.AppendHeader("Access-Control-Allow-Origin", "*");            //JS 跨域访问
            this.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

            return Json(new { name = "dw" },JsonRequestBehavior.AllowGet);      
        }

   function GetList() {

            $.get("http://192.168.2.135:8082/mob/Catalog/GetTest", null, function (ajax) {


                alert(ajax.name);

            }, "json");
        }

 

WCF跨域调用

标签:

原文地址:http://www.cnblogs.com/cdaq/p/4941867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!