Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade
XMLHttpRequest cannot load http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade
使用Ajax 远程 post 到WebService页面的wsGetStreetData方法。报以上错误。
jquery ajax跨域请求,webservice webconfig配置
前台代码:
// 街道的数据
function getStreetData() {
var time1 = $('#time1_street').val(); if (time1 == "" || time1 == null) { alert("请输入值~"); return false; }
var time2 = $('#time2_street').val(); if (time2 == "" || time2 == null) { alert("请输入值~"); return false; }
var data = {
time1: time1,
time2: time2
};
data = JSON.stringify(data);
$.ajax({
type: "post",
url: 'http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData',
async: true,
datatype: "JSONP",
jsonpCallback: "jsonpcallback",
contentType: "application/json",
data: data,
success: function (response) {
var d; d = JSON.parse(response.d);
var jsonData = JSON.stringify(d);
jsonData = jsonData
.replaceAll('"PROJECT_ID":', '')
.replaceAll('"REGION_NAME":', '')
.replaceAll('"TAX":', '')
.replaceAll('"QJSR":', '')
.replaceAll('{', '[')
.replaceAll('}', ']');
jsonData = jsonData.substring(1, jsonData.length - 1);
$('#txtRegionData').val('["id", "街乡", "纳税总额", "区级收入"],' + jsonData);
}
});
}
后台代码:
[WebMethod]
public string wsGetStreetData(string time1, string time2)
{
StringBuilder strsql = new StringBuilder();
strsql.AppendFormat(@"", time1, time2);
DataTable dt = OracleHelper.Query(strsql.ToString()).Tables[]; return JsonConvert.SerializeObject(dt);
}
解决方案:在服务器端的Web.config文件中添加一下内容。
<system.web>
<!--提供Web服务访问方式-->
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>
</system.web>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<modules>
<add name="MyHttpModule" type="WebServiceDemo.MyHttpModule"/>
</modules>
</system.webServer>
</configuration>
如果报错:
未能加载类型“WebServiceDemo.MyHttpModule”。去掉
<modules>
<add name="MyHttpModule" type="WebServiceDemo.MyHttpModule"/>
</modules>
即可。
如果想选定的网站可能跨域访问,修改配置如下:
<add name="Access-Control-Allow-Origin" value="http://domain1.com, http://domain2.com" />
用的时候要注意,这样用可能有风险,具体什么风险还不清楚!!!
此文参考:http://blog.csdn.net/liyifei21/article/details/17509735
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade的更多相关文章
- has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
前端显示: has been blocked by CORS policy: Response to preflight request doesn't pass access control che ...
- .Net Core 处理跨域问题Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
网页请求报错: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Or ...
- 跨域请求错误: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
今天在学习Angular 的HttpInterceptor 拦截器时,发现添加了新的headers键值之后总是报跨域错误.后台使用的是asp.net core. 检查发现,在添加了新的header之后 ...
- ajax post 请求报错Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade
jquery ajax跨域请求,webapi webconfig配置 前台代码(放了一部分) function CheckIn(roomno) { $.ajax({ url: 'https://www ...
- ajax post上传数据时,前端出现的跨域权限问题:ccess to XMLHttpRequest at ‘’rom origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok st
本人前端使用多个框架时,jq ajax传参出现如下报错: 最后发现,可能是xhr的相关默认参数被修改了.顾使用jq 传参时,一直报错,jq ajax额外添加的关键参数: crossDomain: ...
- Js 跨域CORS报错 Response for preflight has invalid HTTP status code 405
问题 公司项目H5调用接口遇到Response for preflight has invalid HTTP status code 405这样的错误,是使用PUT方式提交请求接口.Content-T ...
- Cross-origin resource sharing JSON with Padding 同源策略 JSONP 为什么form表单提交没有跨域问题,但ajax提交有跨域问题? XMLHttpRequest and the Fetch API follow the same-origin policy 预检请求(preflight request)
https://zh.wikipedia.org/wiki/跨来源资源共享 跨来源资源共享(CORS)是一份浏览器技术的规范,提供了 Web 服务从不同域传来沙盒脚本的方法,以避开浏览器的同源策略[1 ...
- 转 Js 跨域CORS报错 Response for preflight has invalid HTTP status code 405
转自:http://www.cnblogs.com/SilenceTom/p/6697484.html 调用接口遇到Response for preflight has invalid HTTP st ...
- CORS & OPTIONS & preflight request
CORS & OPTIONS preflight request CORS 原理 CORS跨域的原理实际上是浏览器与服务器通过一些HTTP协议头来做一些约定和限制 OPTIONS 应用场景 h ...
随机推荐
- oracle调用JAVA类的方法
导入jar包 在oracle中导入需要的jar包,我们把编辑好的java类打成jar包,直接在oarcle里面写简单的调用就可以了, 1.操作系统需要拥有支持loadjava命令的jdk. 2.加 ...
- 查找素数Eratosthenes筛法的mpi程序
思路: 只保留奇数 (1)由输入的整数n确定存储奇数(不包括1)的数组大小: n=(n%2==0)?(n/2-1):((n-1)/2);//n为存储奇数的数组大小,不包括基数1 (2)由数组大小n.进 ...
- 基于H5的微信支付开发详解
这次总结一下用户在微信内打开网页时,可以调用微信支付完成下单功能的模块开发,也就是在微信内的H5页面通过jsApi接口实现支付功能.当然了,微信官网上的微信支付开发文档也讲解的很详细,并且有实现代码可 ...
- wamp下Apache配置vhost
1.由于后面虚拟机中需要用到Rewrite所以先编辑Apache的conf目录下的httpd.conf文件.(我的文件位置是:D:\Program Files\wamp\bin\apache\apac ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- [Penetration Testing Devil Training Camp Based on Metasploit] Learn & Practice
- 【转】虚拟机VMware与主机共享文件介绍
from: http://www.cnblogs.com/kerrycode/p/3818095.html 写的比较详细,但是vm版本较旧. 2:通过共享文件夹功能 虚拟机VMware提供了在宿主机与 ...
- jQuery获取margin-top和padding-top的值
var bordT = $('img').outerWidth() - $('img').innerWidth(); var paddT = $('img').innerWidth() - $('i ...
- Linux-./configure: error: the HTTP rewrite module requires the PCRE library.
这个问题是要解决: yum -y install pcre-devel 然后在yum的时候发现出错.问题是我的linux不能上网. 这个问题搞得烦死了.和主机能ping.但是就是不能上网: ping ...
- vi安装Vundle+YouCompleteMe+注释快捷'scrooloose/nerdcommenter'
Vundle is short for Vim bundle and is a Vim plugin manager. 从git上下载vundle $ git clone https://github ...