jqeury跨域访问WCF自承载的服务

服务端代码:

 

[ServiceContract]
     public  interface IEmployees
    {
        [WebGet(ResponseFormat =WebMessageFormat.Json)]
        IEnumerable<Employee> GetAll(); // UriTemplate = "all",
    }
     public  class Employee
    {
         public  string Id {  getset; }
         public  string Name {  getset; }
         public  string Department {  getset; }
         public  string Grade {  getset; }
    }
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 "}
            };
        }
    }
public  class Program
    {
         static  void Main()
        {
             using (WebServiceHost host =  new WebServiceHost( typeof(EmployeesService),  new Uri( " http://127.0.0.1:3721/employees ")))
            {
                host.Open();
                Console.Read();
            }
        }
    }

 

服务端配置文件:

 

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
   < system.serviceModel >
     < standardEndpoints >
       < webHttpEndpoint >
         < standardEndpoint  crossDomainScriptAccessEnabled ="true" />
       </ webHttpEndpoint >
     </ standardEndpoints >
   </ system.serviceModel >
</ configuration >

客户端代码:

 

<! 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 >
     < style  type ="text/css" >
        body
        
{
            font-size
:  12px ;
            text-align
:  center ;
        
}
        #employees
        
{
            border
:  1px solid #000000 ;
            margin
:  10px auto ;
            background-color
:  #eee ;
        
}
        #employees tr
        
{
            line-height
:  23px ;
        
}
        #employees th
        
{
            background-color
:  #ccc ;
            color
:  #fff ;
        
}
        .oddRow
        
{
            background-color
:  #fff ;
        
}
    
</ style >
     < 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/GetAll " ,
                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 >
</ head >
   < body >
     < table  id ="employees"  width ="600px" >
         < tr >
             < th >ID </ th >
             < th >姓名 </ th >
             < th >级别 </ th >
             < th >部门 </ th >
         </ tr >
     </ table >
</ body >
</ html >

我觉得这样跨域是最简单的。

转载于:https://www.cnblogs.com/fej121/archive/2012/09/30/2709053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值