JSONP(JSON with Padding)可以看成是JSON的一种“使用模式”,用以解决“跨域访问”的问题,这篇简单的文章给出一个简单的例子用于模拟如何通过jQuery以JSONP的访问调用一个WCF REST服务。[源代码从这里下载]
在这个例子中,我们将定义一个用于返回所有员工信息的服务,下面是用于表示员工信息的Employee的类型和契约接口。契约接口IEmployees的GetAll操作用以返回所有员工列表,我们指定了Uri模板并将回复消息格式设置为JSON。
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace Artech.WcfServices.Service.Interface
5: {
6: [ServiceContract]
interface IEmployees
8: {
,ResponseFormat =WebMessageFormat.Json)]
10: IEnumerable<Employee> GetAll();
11: }
class Employee
13: {
string Id { get; set; }
string Name { get; set; }
string Department { get; set; }
string Grade { get; set; }
18: }
19: }