/********************************************************************************** No 'Access-Control-Allow-Origin' header is present on the requested resource.* 说明:*     在php中使用ajax进行跨域访问的过程中遇到这个问题,梦真帮忙解决了。**                                       2016-12-14 深圳 南山平山村 曾剑锋********************************************************************************/一、参考文档:1. XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Originhttp://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin2. 跨域Ajax之ContentType:application/jsonhttp://www.foreverpx.cn/2016/06/22/cross_content_type/3. JQuery 的 ajax 出现Origin null is not allowed by Access-Control-Allow-Origin 解决方法http://blog.csdn.net/leon90dm/article/details/8120378

二、错误现象:Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 501.

三、跨域ajax访问代码:$.ajax({url: "http://192.168.1.20/data.php",type: "POST",contentType:"application/json; charset=utf-8",  //  ---->  问题就在这里了
    data: JSON.stringify(ajaxPostData),dataType:"json",success: function(data){//On ajax success do thisconsole.info("success.");if (data["status"] == "ok"){alert("Settings is Ok. The Machine is rebooting.");}},error: function(xhr, ajaxOptions, thrownError) {//On error do thisconsole.info("error.");if (xhr.status == 200) {alert(ajaxOptions);}else {alert(xhr.status);alert(thrownError);}}
});四、问题原因:在使用Ajax跨域请求时,如果设置Header的ContentType为application/json,会分两次发送请求。第一次先发送Method为OPTIONS的请求到服务器,这个请求会询问服务器支持哪些请求方法(GET,POST等),支持哪些请求头等等服务器的支持情况。等到这个请求返回后,如果原来我们准备发送的请求符合服务器的规则,那么才会继续发送第二个请求,否则会在Console中报错。五、解决办法:1. 在ajax访问中不指定contentType:"application/json; charset=utf-8",使用默认的就可以了;2. 如下代码:$.ajax({url: "http://192.168.1.20/data.php",type: "POST",data: JSON.stringify(ajaxPostData),dataType:"json",success: function(data){//On ajax success do thisconsole.info("success.");if (data["status"] == "ok"){alert("Settings is Ok. The Machine is rebooting.");}},error: function(xhr, ajaxOptions, thrownError) {//On error do thisconsole.info("error.");if (xhr.status == 200) {alert(ajaxOptions);}else {alert(xhr.status);alert(thrownError);}}});

转载于:https://www.cnblogs.com/zengjfgit/p/6178189.html

No 'Access-Control-Allow-Origin' header is present on the requested resource.相关推荐

  1. header is present on the requested resource. Origin 'null' is therefore not allowed access.

    No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is there ...

  2. js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource

    js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...

  3. 解决xxx by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested resource问题

    文章目录 问题复现 跨域现象 什么是跨域 为什么要跨域 同源策略 什么是同源策略 为何存在同源策略 解决跨域问题 其他解决跨域问题的方案 JSONP CORS 跨域资源共享 前端设置 服务器设置 反向 ...

  4. 解决springboot跨域问题No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

    文章目录 1.问题描述 2.问题产生 3.解决方案 1. 在WebMvcConfig添加(推荐使用) 2.直接采用SpringBoot的注解@CrossOrigin 1.Controller层在需要跨 ...

  5. 新版本Chrome同源策略、跨域问题处理No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

    本文的方法实用于本地调试浏览器的设置,如果想要彻底解决需要使用 CORS(跨域资源共享) [ 相关文章 ] Python 使用 CORS 跨域资源共享解决 flask 服务器跨域问题.浏览器同源策略 ...

  6. 错误记录(12):No 'Access-Control-Allow-Origin' header is present on the requested resource.

    背景 使用Python微框架flask写接口时,请求某个方法需要返回一些json数据.本机没有任何问题,当需要前后端分离时,做内网穿透,其他机子请求这个接口会报错,错误提示: Failed to lo ...

  7. No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

    [解决跨域问题] Access to XMLHttpRequest at 'http://localhost:3000/' from origin 'null' has been blocked by ...

  8. 报错:跨域问题解决 No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

    报错: Access to XMLHttpRequest at 'http://127.0.0.1:8088/user/list' from origin 'http://localhost:8080 ...

  9. Vue 跨域请求报错No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

    坑是一个一个踩出来的... 报错如下: Access to XMLHttpRequest at 'http://apps.eshimin.com/traffic/gjc/getArriveBase?n ...

最新文章

  1. PowerPath/VE:EMC夯实虚拟环境的独门武器
  2. C语言初始化错误怎么办,结构体变量的初始化错误
  3. Docker实践:Centos下安装Docker并简单的使用
  4. 学习型php空间,补装修日记:学习型业主三大要素
  5. html文档打开错误,我的html的文档无法打开,打开也是报错
  6. Zookeeper Learning
  7. python中的多线程、多进程
  8. [POJ 1742] Coins 【DP】
  9. 16、Flutter Widget - PageView;
  10. ISP图像调试工程师——3D和2D降噪(熟悉图像预处理和后处理技术)
  11. android实现资源嗅探器手机版,OPPO Reno尝鲜Android Q教程来了
  12. 已取消到该网页的导航 桌面右下角
  13. 被告知孩子学校偷钱后
  14. MySQL数据库软件介绍
  15. 【8019】俞军给淘宝产品经理的分享
  16. 1024分辨率《X战警:第一战》BD中英双字无水印
  17. DeepMind《星际争霸2》AI碾压人类遭Gary Marcus猛怼:通用智能就是空谈
  18. 近红外光谱、拉曼光谱及高光谱的区别
  19. 2015 年最受欢迎的 7 个系统监控工具
  20. 新网站需要多少时间才有收录量?

热门文章

  1. 不可不看的干货——机器人自主系统的技术构建:感知、决策和执行
  2. 蝙蝠为啥这么厉害?地球人整明白了没有?
  3. 什么是智能仓储?一文带你彻底搞懂!
  4. 一文告诉你,谷歌是否真的实现了「量子至上」
  5. 宜宾地震,这个系统跑赢了“地震波”!
  6. 程序员圈的鄙视链 | 每日趣闻
  7. 或许不理解,但你一定说过的游戏黑话
  8. 重回大厂 996 的年轻人:还是先搞钱
  9. 漏答题、偏科、多次复读,30 位互联网大佬考上啥大学了?
  10. EOS主网上线只是开始,如何运营决定未来