目录

1       大概思路... 1

2       创建WebAPI 1

3       创建CrossMainController并编写... 1

4       Nuget安装microsoft.aspnet.webapi.cors. 4

5       跨域设置路由... 4

6       编写Jquery EasyUI界面... 5

7       运行效果... 7

8       总结... 7

1       大概思路

l  创建WebAPI

l  创建CrossMainController并编写

l  Nuget安装microsoft.aspnet.webapi.cors

l  跨域设置路由

l  编写Jquery EasyUI界面

l  运行效果

2       创建WebAPI

创建WebAPI,新建->项目->ASP.NET Web应用程序->Web API

3       创建CrossMainController并编写

编写如下:

using CrossdomainWebApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http; namespace CrossdomainWebApi.Controllers
{
[RoutePrefix("api/CrossMain")]
public class CrossMainController : ApiController
{
[Route("GetUserInfo")]
[HttpPost]
public HttpResponseMessage GetUserInfo([FromBody]formUserInfo obj)
{
//[FromBody]int page, [FromBody]int rows, [FromBody]string email
List<UserInfoViewModel> listStudent = new List<UserInfoViewModel>();
for (int i = ; i < ; i++)
{
UserInfoViewModel student = new Models.UserInfoViewModel();
Random ran = new Random();
student.Email = i.ToString() + ran.Next(, ).ToString() + "System@qq.com";
student.HasRegistered = true;
student.LoginProvider = "Yes";
listStudent.Add(student);
} int page = obj.page;
int rows = obj.rows;
List<UserInfoViewModel> ts = new List<UserInfoViewModel>();
for (int i = (page - ) * rows; i < (page * rows>listStudent.Count? listStudent.Count:page * rows) ; i++)
{
ts.Add(listStudent[i]);
} string json= Newtonsoft.Json.JsonConvert.SerializeObject(new { rows = ts, total = listStudent.Count, success = true });
return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "text/plain") };
}
} /// <summary>
/// form提交数据
/// </summary>
public class formUserInfo
{
public int page { get; set; }
public int rows { get; set; }
public string email { get; set; }
}
}

启动运行:http://localhost:26735/help

4       Nuget安装microsoft.aspnet.webapi.cors

5       跨域设置路由

设置项目 crossdomainwebapi\crossdomainwebapi\app_start\webapiconfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
using Newtonsoft.Json.Serialization;
//跨域引用
using System.Web.Http.Cors; namespace CrossdomainWebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
//config.SuppressDefaultHostAuthentication();
//config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); //跨域配置
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
// Web API routes
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}

6       编写Jquery EasyUI界面

前台界面代码如下:

<!DOCTYPE HTML>
<html>
<head>
<title>Ems SA</title>
<link rel="stylesheet" type="text/css" href="JqueryEasyui/themes/bootstrap/easyui.css" />
<link rel="stylesheet" type="text/css" href="JqueryEasyui/themes/icon.css" />
<link rel="stylesheet" type="text/css" href="JqueryEasyui/demo/demo.css" />
<script type="text/javascript" src="JqueryEasyui/jquery.min.js"></script>
<script type="text/javascript" src="JqueryEasyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
function doSearch() { }
</script>
</head>
<body>
<div>
<table id="dg" class="easyui-datagrid" style="width: 100%; height: auto; min-height: 400px"
data-options="
iconCls: 'icon-edit',
singleSelect: true,
url: 'http://localhost:26735/Api/CrossMain/GetUserInfo',
method: 'post',
pagination:true,
pageSize:15,
pageList: [5, 10, 15],
queryParams: {'email': ‘’}
">
<thead>
<tr>
<th data-options="field:'ck',checkbox:true">
</th>
<th data-options="field:'Email',width:'20%'">
Email
</th>
<th data-options="field:'HasRegistered'">
HasRegistered
</th>
<th data-options="field:'HasRegistered'">
HasRegistered
</th>
</tr>
</thead>
</table>
</div>
</body>
</html>

7       运行效果

启动WebAPI,并刷新Jquery EasyUI界面,可见如下图:

8       总结

WebAPI提供广泛的对外开放,可以起到整合性的作用,例如:跟Oracle ERP、SAP的SCM、MM、PP以及SD领域。WebAPI形成一个Web标准,对于一些微服务,也起到关键性的作用。跨域还能不受限制让更多人访问,当然,也降低了安全性。

源代码下载:

http://download.csdn.net/download/ruby_matlab/10117635

PDF下载:

跨域WebApi的Jquery EasyUI的数据交互pdf

跨域WebApi的Jquery EasyUI的数据交互的更多相关文章

  1. 浅析Ajax跨域原理及JQuery中的实现分析

    AJAX 的出现使得网页可以通过在后台与服务器进行少量数据交换,实现网页的局部刷新.但是出于安全的考虑,ajax不允许跨域通信.如果尝试从不同的域请求数据,就会出现错误.如果能控制数据驻留的远程服务器 ...

  2. Ajax跨域原理及JQuery中的实现

    浅析Ajax跨域原理及JQuery中的实现分析   AJAX 的出现使得网页可以通过在后台与服务器进行少量数据交换,实现网页的局部刷新.但是出于安全的考虑,ajax不允许跨域通信.如果尝试从不同的域请 ...

  3. 扩充 jQuery EasyUI Datagrid 数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)

    客户需求: jQuery EasyUI Datagrid 用户列表鼠标悬停/离开数据行时显示人员头像(onMouseOver/onMouseOut) 如图所示,Datagrid 鼠标悬停/离开数据行时 ...

  4. 扩展 jQuery EasyUI Datagrid 数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)

    客户需求: jQuery EasyUI Datagrid 用户列表鼠标悬停/离开数据行时显示人员头像(onMouseOver/onMouseOut) 如图所示,Datagrid 鼠标悬停/离开数据行时 ...

  5. ASP.NET 跨域请求之jQuery的ajax jsonp的使用解惑 (转载)

    前天在项目中写的一个ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,直接执行了error方法提示错误——ajax jsonp之前并没有用过,对其的理解为跟普 ...

  6. ajax 跨域webapi 最简单的demo(只介绍Get)

    这几天遇到一个nodejs的项目,使用VSCode开发,需要连接数据库的,但是用nodejs连接数据库比较繁琐,需要安装很多东西,本人也懒得去研究了.后来想到建一个WebAPI然后用ajax来调用,避 ...

  7. JSONP跨域原理和jQuery.getJSON用法

    JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式).本文主要介绍JS ...

  8. 跨域问题及jQuery中Ajax传参的讲解

    1.跨域:不再同一服务器下,就是协议,域名,端口,有一个不一样: 浏览器对于javascript的同源策略的限制: 案例: 以 http://172.164.23:8088/ 为例 相同域名:172. ...

  9. 套用JQuery EasyUI列表显示数据、分页、查询

    声明,本博客从csdn搬到cnblogs博客园了,以前的csdn不再更新,朋友们可以到这儿来找我的文章,更多的文章会发表,谢谢关注! 有时候闲的无聊,看到extjs那么肥大,真想把自己的项目改了,最近 ...

随机推荐

  1. 移动 云MAS 发短信 .net HTTP 请求

    本人开发移动云MAS .net Http 请求  代码如下 using Newtonsoft.Json.Linq; using System; using System.Collections.Gen ...

  2. Uva 12436 Rip Van Winkle&#39;s Code

    Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...

  3. hdu4416 Good Article Good sentence (后缀数组)

    题意:问a串中有多少种字符串集合B中没有的连续子串. a的长度10^5,B中的总长度为不超过10^5. 解法:后缀数组题目:后缀数组能够非常easy算出来一个串中有多少种子串. 把a和B集合连起来.求 ...

  4. 浏览器正确理解和使用GBK及UTF-8(UTF-8 + BOM)网页编码

    网页编码英文译为web page encoding.是在网页中指定其特定的字符编码格式的库. GBK是国家标准GB2312基础上扩容后兼容GB2312的标准. GBK的文字编码是用双字节来表示的.即不 ...

  5. 【Jquery系列】JqGrid参数详解

    1   概述 本篇文章主要与大家分享JqGrid插件参数问题. 2   参数详解 2.1 初始化参数 2.2  ColModel参数 3   json数据 jqGrid可支持的数据类型:xml.jso ...

  6. D01 Elon Mulsk The future we're building — and boring

    摘要:精选TED. 每个音频不超过2分钟,学英语和吸收伟大思想两不误 音频: https://n1audio.hjfile.cn/st/fb5ace6f-7b63-439d-954c-c4539c1f ...

  7. CS:APP3e 深入理解计算机系统_3e Attacklab 实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  8. OC学习9——反射机制

    1.OC提供了3种编程方式与运行环境进行交互: 直接通过OC的源代码:这是最常见的方式,开发人员只是编写OC源代码,而运行环境负责在后台工作. 通过NSObject类中定义的方法进行动态编程:因为绝大 ...

  9. HTTP Live Streaming 直播(iOS直播) 初识

    HTTP Live Streaming(HLS)技术,并实现了一个HLS编码器HLSLiveEncoder,当然,C++写的.其功能是采集摄像头与麦克风,实时进行H.264视频编码和AAC音频编码,并 ...

  10. 免费SSL&付费SSL证书,该如何选择?

    近年来Google.Apple.百度等公司不断推动 HTTPS 的普及,SSL 证书作为 HTTPS 安全协议的必备配置,自然也成为了网站.App 开发者最重要部署项目之一. 又拍云于 2016 年联 ...