跨域WebApi的Jquery EasyUI的数据交互
目录
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%'">
</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的数据交互的更多相关文章
- 浅析Ajax跨域原理及JQuery中的实现分析
AJAX 的出现使得网页可以通过在后台与服务器进行少量数据交换,实现网页的局部刷新.但是出于安全的考虑,ajax不允许跨域通信.如果尝试从不同的域请求数据,就会出现错误.如果能控制数据驻留的远程服务器 ...
- Ajax跨域原理及JQuery中的实现
浅析Ajax跨域原理及JQuery中的实现分析 AJAX 的出现使得网页可以通过在后台与服务器进行少量数据交换,实现网页的局部刷新.但是出于安全的考虑,ajax不允许跨域通信.如果尝试从不同的域请 ...
- 扩充 jQuery EasyUI Datagrid 数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)
客户需求: jQuery EasyUI Datagrid 用户列表鼠标悬停/离开数据行时显示人员头像(onMouseOver/onMouseOut) 如图所示,Datagrid 鼠标悬停/离开数据行时 ...
- 扩展 jQuery EasyUI Datagrid 数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)
客户需求: jQuery EasyUI Datagrid 用户列表鼠标悬停/离开数据行时显示人员头像(onMouseOver/onMouseOut) 如图所示,Datagrid 鼠标悬停/离开数据行时 ...
- ASP.NET 跨域请求之jQuery的ajax jsonp的使用解惑 (转载)
前天在项目中写的一个ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,直接执行了error方法提示错误——ajax jsonp之前并没有用过,对其的理解为跟普 ...
- ajax 跨域webapi 最简单的demo(只介绍Get)
这几天遇到一个nodejs的项目,使用VSCode开发,需要连接数据库的,但是用nodejs连接数据库比较繁琐,需要安装很多东西,本人也懒得去研究了.后来想到建一个WebAPI然后用ajax来调用,避 ...
- JSONP跨域原理和jQuery.getJSON用法
JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式).本文主要介绍JS ...
- 跨域问题及jQuery中Ajax传参的讲解
1.跨域:不再同一服务器下,就是协议,域名,端口,有一个不一样: 浏览器对于javascript的同源策略的限制: 案例: 以 http://172.164.23:8088/ 为例 相同域名:172. ...
- 套用JQuery EasyUI列表显示数据、分页、查询
声明,本博客从csdn搬到cnblogs博客园了,以前的csdn不再更新,朋友们可以到这儿来找我的文章,更多的文章会发表,谢谢关注! 有时候闲的无聊,看到extjs那么肥大,真想把自己的项目改了,最近 ...
随机推荐
- select联动遇到的问题
今天写了个select下拉联动,记录下过程中遇到的问题. dom部分 // 拿到选中项的索引 myselect.selectedIndex // 拿到选中项的text/value myselect.o ...
- 《3》CentOS7.0+OpenStack+kvm云平台部署—配置Glance
感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免,欢迎指正. 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- SSRS Fields cannot be used in page headers or footers
问题环境:SSRS 2005 报表开发 尝试在Page Header中使用Data Set的字段,遇到以下的错误: The value expression for textbox '' refers ...
- javaweb:推断当前请求是否为移动设备訪问
这段时间都是在做pc端的业务.结果经理找到我说,可能要做移动端的应用,后台逻辑还是用我写的逻辑.可是须要修改一些,看看怎么处理. 因为移动端和pc端还是略微有些差别的,我认为最好是在一个地儿统一推断, ...
- Ubuntu 下使用 ZTE ME3630 4G 模块
之前在 AM5728 开发板上使用过这个模块,用来在野外采集数据上传到服务器.最近接触另外一个项目,做一个演示用的样机,需要移动的,也是采用了这个模块来上传数据.样机环境是 Ubuntu 16.04 ...
- 虚拟机修改静态ip
1.设置虚拟机的网络选择方式,使用NAT模式,选择这个模式后网段与主机的网段不是一个网段,一般选择桥接模式 2.选择VMnet8, 去掉 使用本地DHCP服务将ip分配给虚拟机 这个选项,不然设置ip ...
- 一次对象过大引起的gc性能问题的分析与定位
现象:一个接口在4C的机器上跑最大只有7TPS,CPU使用率就已经90%多. 定位: 1. 使用top命令查看CPU使用情况,找到进程号 2. 使用top -H -pid命令,查看进程信息,看到有 ...
- Django实战,小网站实现增删改查
直接上代码 视图: from django.shortcuts import render,render_to_response, redirect from submit import models ...
- 【model模型传入view的数据类型错误】传入字典的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery`1[MapScience.PovertyAlleviation.Web.Models.Qu
出现这个问题的原因是控制器中传给view的数据类型与View中设置的model类型不一致导致,比如控制器返回的IList类型的,而你在View里面model设置的是IEnumerable<> ...
- softmax分类算法原理(用python实现)
逻辑回归神经网络实现手写数字识别 如果更习惯看Jupyter的形式,请戳Gitthub_逻辑回归softmax神经网络实现手写数字识别.ipynb 1 - 导入模块 import numpy as n ...