MVC跨域API
API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using yue5API.Models;
namespace yue5API.Controllers
{
[EnableCors("*", "*", "*")]
public class BookApiController : ApiController
{
// GET: api/BookApi
yue5DBEntities db = new yue5DBEntities();
public IEnumerable<stu> Get()
{
return db.stus.ToList();
}
// GET: api/BookApi/5
public stu Get(int id)
{
var tt = db.stus.Where(s => s.ID == id).FirstOrDefault();
return tt;
}
// POST: api/BookApi
public void Post([FromBody]stu value)
{
db.stus.Add(value);
db.SaveChanges();
}
// PUT: api/BookApi/5
public void Put(int id, [FromBody]stu value)
{
var tt = db.stus.Where(s => s.ID == id).FirstOrDefault();
if (tt != null)
tt.ID = value.ID;
tt.Name = value.Name;
tt.Num = value.Num;
tt.Price = value.Price;
db.SaveChanges();
}
// DELETE: api/BookApi/5
public HttpResponseMessage Delete(int id)
{
var tt = db.stus.Where(s => s.ID == id).FirstOrDefault();
if (tt != null)
{
db.stus.Remove(tt);
db.SaveChanges();
return new HttpResponseMessage() { StatusCode = HttpStatusCode.OK };
}
else
{
return new HttpResponseMessage() { StatusCode = HttpStatusCode.NoContent };
}
}
}
}
MVC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using yue5MVC.Models;
using Newtonsoft.Json;
namespace yue5MVC.Controllers
{
public class showController : Controller
{
//显示
// GET: show
public ActionResult Index()
{
Uri uri = new Uri("http://localhost:4970");
HttpClient client = new HttpClient();
client.BaseAddress = uri;
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message=client.GetAsync("api/BookApi").Result;
List<All> tt = new List<All>();
if (message.IsSuccessStatusCode)
{
string pp = message.Content.ReadAsStringAsync().Result;
tt = JsonConvert.DeserializeObject<List<All>>(pp);
}
client.Dispose();
return View(tt);
}
public ActionResult add()
{
return View();
}
//删除
public ActionResult shan(int id)
{
Uri uri = new Uri("http://localhost:4970");
HttpClient client = new HttpClient();
client.BaseAddress = uri;
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message = client.DeleteAsync("api/BookApi/"+id).Result;
if (message.IsSuccessStatusCode)
{
return Content("<script>alert('删除成功');location.href='/show/Index'</script>");
}
else
{
return Content("<script>alert('删除失败')</script>");
}
}
//修改
public ActionResult xiu(int id)
{
ViewBag.id = id;
return View();
}
}
}
前台修改
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>xiu</title>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
</head>
<body>
<div>
<p>名称:<input id="txtname" type="text" name="Name" /></p>
<p>数量:<input id="txtnum" type="text" name="Num" /></p>
<p>价格:<input id="txtprice" type="text" name="Price" /></p>
<input id="Submit1" type="submit" value="sub修改" />
<input id="Button1" type="button" value="修改" onclick="xiu()" />
<script>
$(function () {
show();
})
function show() {
var id = '@ViewBag.id';
$.ajax({
url: "http://localhost:4970/api/Bookapi/"+id,
type: "Get",
success: function (data) {
$("#txtname").val(data.Name);
$("#txtnum").val(data.Num);
$("#txtprice").val(data.Price);
}
})
}
function xiu() {
var id = '@ViewBag.id';
$.ajax({
url: "http://localhost:4970/api/Bookapi/"+id,
type: "Put",
data:{ID:id,Name:$("#txtname").val(),Num:$("#txtnum").val(),Price:$("#txtprice").val()},
success: function (data) {
alert("修改成功");
location.href = '/show/Index';
}
})
}
</script>
</div>
</body>
</html>
添加
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>add</title>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
</head>
<body>
<div>
<p>名称:<input id="txtname" type="text" /></p>
<p>数量:<input id="txtnum" type="text" /></p>
<p>价格:<input id="txtprice" type="text" /></p>
<input id="Button1" type="button" value="添加" onclick="tian()" />
<script>
function tian() {
$.ajax({
url: "http://localhost:4970/api/Bookapi",
type: "Post",
dataType: "json",
data: { Name: $("#txtname").val(), Num: $("#txtnum").val(), Price: $("#txtprice").val() },
success: function (data) {
alert("添加成功");
location.href = '/show/Index';
}
})
}
</script>
</div>
</body>
</html>
显示
@{
Layout = null;
}
@model List<yue5MVC.Models.All>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
</head>
<body>
<div>
<table>
<tr>
<td>名称</td>
<td>数量</td>
<td>价格</td>
<td>操作</td>
</tr>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.Num</td>
<td>@item.Price</td>
<td><a href="#" onclick="shan(@item.ID)">删除</a>
<a href="/show/xiu/@item.ID">修改</a></td>
</tr>
}
</tbody>
</table>
<table id="show"></table>
<script>
$(function () {
show();
})
function show() {
$.ajax({
url: "http://localhost:4970/api/Bookapi/"+id,
type: "Get",
success: function (data) {
var str = "";
str += "<tr><td>" + data.Name+ "</td></tr>";
str += "<tr><td>" + data.Num + "</td></tr>";
str += "<tr><td>" + data.Price + "</td></tr>";
$("#show").val();
}
})
function shan(id){
location.href='/show/shan?id='+id;
}
</script>
</div>
</body>
</html>
MVC跨域API的更多相关文章
- 跨域API
跨域API 简单跨域请求 只需要简单的设置允许跨域就可以了 def set_default_headers(self): self.set_header('Access-Control-Allow-O ...
- Angular2中对ASP.NET MVC跨域访问
应用场景 项目开发决定使用angular2进行前后端分离开发,由我负责后端服务的开发,起初选择的是web api进行开发.对跨域访问通过API中间件+过滤器对跨域访问进行支持.开发一段后,通知需要移植 ...
- MVC跨域CORS扩展
一般的基于浏览器跨域的主要解决方法有这么几种:1.JSONP 2.IFrame方式 3.通过flash实现 4.CORS跨域资源共享 ,这里我们主要关注的是在MVC里面的CORS ...
- 关于Spring MVC跨域
1.Sping MVC 3.X跨域 关于跨域问题,主要用的比较多的是cros跨域. 详细介绍请看https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Ac ...
- spring mvc跨域设置(全局)
//--------------第一步//spring 5版本全局配置方式 @Configuration @EnableWebMvc public class SpringMvcBeans imple ...
- spring mvc 跨域问题。。。解决
官方推荐方式: http://spring.io/blog/2015/06/08/cors-support-in-spring-framework 方式1: $.ajax({ //前台:常规写法.注意 ...
- spring mvc 跨域请求处理——spring 4.2 以上
Controller method CORS configuration You can add to your @RequestMapping annotated handler method a ...
- Asp.Net 跨域,Asp.Net MVC 跨域,Session共享,CORS,Asp.Net CORS,Asp.Net MVC CORS,MVC CORS
比如 http://www.test.com 和 http://m.test.com 一.简单粗暴的方法 Web.Config <system.web> <!--其他配置 省略……- ...
- 解决.Net Mvc跨域请求问题
针对ASP.NET MVC和ASP.NET Web API两种项目类型 1.针对ASP.NET MVC,只需要在web.config中添加如下的内容即可 <system.webServer> ...
随机推荐
- C++重载操作符自增自减
#include <iostream> using namespace std; class Test { friend ostream& operator<<(ost ...
- 【一些容易忘记的node的npm命令】【收集】
更新npm到最新版本 npm update -g npm 安装依赖包时命令的一些区别 npm install xxx -g //(全局安装) npm install xxx --save-dev // ...
- phpstorm2018激活方法
直接用浏览器打开 http://idea.lanyus.com/点击页面中的“获得注册码”,然后在注册时切换至Activation Code选项,输入获得的注册码一长串字符串如果提示红字体信息,那么先 ...
- java excel大数据量导入导出与优化
package com.hundsun.ta.utils; import java.io.File; import java.io.FileOutputStream; import java.io.I ...
- Java虚拟机内存分配详解
简介 了解Java虚拟机内存分布的好处 1.了解Java内存管理的细节,有助于程序员编写出性能更好的程序.比如,在新的线程创建时,JVM会为每个线程创建一个专属的栈 (stack),其栈是先进后出的数 ...
- 虚拟主机、VPS、ECS云服务器 区别
在阿里云上买了个云服务器. ssh命令都没通,找服务端同事帮我看,说我买错了.应该买ECS. 1.虚拟主机 虚拟主机就是利用虚拟化的技术,将一台服务器划分出一定大小的空间,每个空间都给予单独的 FTP ...
- Ubuntu 16.04下vsftpd 安装配置实例
从https://www.linuxidc.com/Linux/2017-06/144807.htm转载 第一步:安装VSFTPD sudo apt-get install vsftpd 安装完成后启 ...
- SQLServer “无法对数据库'XX' 执行删除,因为它正用于复制”的解决方法
修改数据库某个字段的长度时出现: “无法修改表.无法对 表'dbo.N_Client_content' 执行 删除,因为它正用于复制.” 不能直接对该数据库进行操作,通过alter 的办法来修改,问题 ...
- 阿里技术专家详解Dubbo实践,演进及未来规划
https://mp.weixin.qq.com/s/9rVGHYfeE8yM2qkSVd2yEQ
- spring初体验 一之helloworld
今天开始学习spring,每天都会将自己学习的一些内容,或是一些总结以博客的形式记录下来,方便自己以后回顾,如果能给他人学习带来丁点的帮助那也是最好不过了.本系列博文的spring学习是基于4.0版本 ...