c# 跨域api
前端 ajax get请求
$.ajax({
url: "API地址",
type: 'get',
dataType: 'jsonp',
async: true,
processData: true,
contentType: false,
success: function (res) {
console.log(res)
},
error: function (Error) {
console.log(Error)
}
})
前端 ajax post请求
$.ajax({
type: 'post',
url: 'API地址',
data: data,
dataType: "json",
crossDomain: true,
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
c# 后端 get请求
var res="最好转换成json";
string callback = this.Request["callback"];
return Content(callback + "(" + res + ")");
c# 后端 post 请求
//1.可以创建一个FilterAttribute
public class PostAPIController : ActionFilterAttribute
{
//override OnActionExecuting
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//设置
filterContext.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
filterContext.HttpContext.Response.Headers.Add("Access-Control-Allow-Methods", "POST");
filterContext.HttpContext.Response.Headers.Add("Access-Control-Allow-Headers", "x-requested-with,content-type"); base.OnActionExecuting(filterContext);
}
}
//2.post请求代码块 加上过滤器 PostAPIController
[HttpPost]
[PostAPIController]
public ActionResult AddReadNum(int newid)
{
try
{
*******
*******
if (string.IsNullOrEmpty(callback))
{
return Json(result, JsonRequestBehavior.AllowGet);
}
else
{
string res = JsonToots.ModelToJson(result);
return Content(callback + "(" + res + ")"); } }
catch (Exception ex)
{
******
}
}
c# 跨域api的更多相关文章
- 跨域API
跨域API 简单跨域请求 只需要简单的设置允许跨域就可以了 def set_default_headers(self): self.set_header('Access-Control-Allow-O ...
- MVC跨域API
API using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Ne ...
- php跨域请求
跨域api服务器设置 header('content-type:application:json;charset=utf8'); header('Access-Control-Allow-Origin ...
- Ajax跨域问题
为了数据安全,默认情况下ajax请求是不支持跨域访问. 但是可以通过以下方法让ajax接收跨域API地址 第一种:JSONP比较老,较简单的请求可以考虑使用. JS代码 $(function () { ...
- 【Vue+Node】解决axois请求数据跨域问题
项目基于Vue前端+Node后台,启动两个服务,请求数据时,端口不一致造成跨域报错: (No 'Access-Control-Allow-Origin' header is present on th ...
- Vue使用vue-recoure + http-proxy-middleware + vuex配合promise实现基本的跨域请求封装
使用vue init webpack 你的项目名称初始化一个vue的项目 安装依赖 npm install vue-resource http-proxy-middleware vuex koa 在项 ...
- 原创:【ajax | axios跨域简单请求+复杂请求】自定义header头Token请求Laravel5后台【亲测可用】
如标题:我想在ajax的header头增加自定义Token进行跨域api认证并调用,api使用laravel5编写,如何实现? 首先,了解下CORS简单请求和复杂请求. -- CORS简单请求 -- ...
- springboot+vue2.x 解决session跨域失效问题
服务端SpringBoot2.x :localhost:8082 前端Vue2.x :localhost:81 前后端的端口号不同,为跨域,导致前端访问后端时,每次 ...
- uni-app运行到浏览器跨域H5页面的跨域问题解决方案
官方文档对跨域的解决方案推荐: https://ask.dcloud.net.cn/article/35267 更方便的解决方案 项目根目录直接创建一个vue.config.js文件,并在里面配置代理 ...
随机推荐
- min cost max flow算法示例
问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...
- ASP.NET判断是否为手机登录
protected void Page_Load(object sender, EventArgs e) { MobileHandle(); } 页面加载时候判断是否为手机登录 protected v ...
- Method not found: 'System.Data.Entity.ModelConfiguration.Configuration.XXX
使用EF flument API 修改映射数据库字段的自增长 modelBuilder.Entity<Invoice>().Property(p => p.Id).HasDatab ...
- iis支持asp.net4.0的注册命令使用方法
32位的Windows: 1. 运行->cmd 2. cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 3. aspnet_regiis.exe ...
- 《Linux-基础篇笔记》 Vim编辑器(二)
Linux图形化界面下的文本编辑器 gedit . libre office . evince PDF阅读器 ①gedit是一个GNOME桌面环境下兼容UTF-8的文本编辑器.它使用GTK+编写而成, ...
- Select count(*)、Count(1)、Count(0)的区别和执行效率比较
记得很早以前就有人跟我说过,在使用count的时候要用count(1)而不要用count(*),因为使用count(*)的时候会对所有的列进行扫描,相比而言count(1)不用扫描所有列,所以coun ...
- 【Spark算子】:reduceByKey、groupByKey和combineByKey
在spark中,reduceByKey.groupByKey和combineByKey这三种算子用的较多,结合使用过程中的体会简单总结: 我的代码实践:https://github.com/wwcom ...
- golang 闭包求斐波那契数列
题目是Go指南中的闭包求斐波那契数列 package main import "fmt" // 返回一个"返回int的函数" func fibonacci() ...
- IQueryable与IEnumerable
IEnumerable: 从服务器处取回所有数据,在客户端根据过滤条件进行过滤再返回结果. IQueryable: 从服务器处进行过滤,直接返回过滤后的结果.
- [原创]K8飞刀 新增Acunetix WVS 远程漏洞 反制黑客
工具: K8飞刀20150603组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.blog.163.com发布: 2015/6/3 20:41:29 简介: ...