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的更多相关文章

  1. 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 ...

  2. .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 ...

  3. 跨域请求错误: 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之后 ...

  4. 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 ...

  5. 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: ...

  6. Js 跨域CORS报错 Response for preflight has invalid HTTP status code 405

    问题 公司项目H5调用接口遇到Response for preflight has invalid HTTP status code 405这样的错误,是使用PUT方式提交请求接口.Content-T ...

  7. 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 ...

  8. 转 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 ...

  9. CORS & OPTIONS & preflight request

    CORS & OPTIONS preflight request CORS 原理 CORS跨域的原理实际上是浏览器与服务器通过一些HTTP协议头来做一些约定和限制 OPTIONS 应用场景 h ...

随机推荐

  1. javascript-模板方法模式-提示框归一化插件

    模板方法模式笔记   父类中定义一组算法操作骨架,而将一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤   实例:弹出框归一化插件 css样式 ;width ...

  2. jquery的几个常用方法

    第一部份关键词: .bind() .unbind() .css() .hasclass() .removeclass .parent() .children() .html() .hide() .sh ...

  3. uboot的配置流程分析

    简单介绍一下uboot的基本配置流程.和绝大多数源码编译安装一样,uboot在执行make之前需要执行make XXXconfig来配置相关信息,而且uboot本身是针对多种平台的bootloader ...

  4. pptpvpn 连接后 无法上外网

    VPN拨号后只能连内网,不能上外网的原因和解决方法 因为众所周知的原因,很多个人或者单位需要架设自己的VPN.会遇到一些奇怪的情况,比如说:为什么连上单位的VPN后就只好上公司的内网(局域网),外网( ...

  5. [转].net core 通过ViewComponent封装控件 左侧菜单

    本文转自:http://www.cnblogs.com/BenDan2002/p/6224816.html 我们在.net core中还使用了ViewComponent方式生成控件.ViewCompo ...

  6. 安装CentOS7文字界面版后,无法联网,用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法

    *无法联网的明显表现会有: 1.yum install出现 Error: cannot find a valid baseurl or repo:base 2.ping host会提示unknown ...

  7. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  8. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. 如何在一台新电脑上配置JAVA开发环境

    对于JAVA新手来说,刚开始要学JAVA,而自己的电脑上毫无与JAVA开发有关的环境,应该如何进行配置呢? (安卓新手也需要JAVA开发环境) 第一步,下载.安装java JRE JRE (Java ...

  10. 开发 ASP.NET vNext 初步总结(使用Visual Studio 14 CTP1)

    新特性: vNext又称MVC 6.0,不再需要依赖System.Web,占用的内存大大减少(从前无论是多么简单的一个请求,System.Web本身就要占用31KB内存). 可以self-host模式 ...