同一域上的CORS错误?

2023-04-18前端开发问题
23

本文介绍了同一域上的CORS错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我现在遇到了一个奇怪的 CORS 问题.

I'm running into a weird CORS issue right now.

这是错误信息:

XMLHttpRequest cannot load http://localhost:8666/routeREST/select?q=[...] 
Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin

两台服务器:

  • localhost:8666/routeREST/:这是一个简单的 Python Bottle 服务器.
  • localhost:8080/:Python simpleHTTPserver,我在其中运行 y Javascript 应用程序.此应用正在上面的服务器上执行 Ajax 请求.

有没有想过可能是什么问题?

Any thought on what could be the problem?

而且...端口是问题所在.谢谢你的回答:)

And... the port was the problem. Thanks for your answers :)

如果有人也在使用 Python 瓶服务器,您可以按照这篇文章中给出的答案来解决 CORS 问题:Bottle Py:为 jQuery AJAX 请求启用 CORS

If anyone is using a Python bottle server as well, you can follow the answer given on this post to solve the CORS issue: Bottle Py: Enabling CORS for jQuery AJAX requests

推荐答案

只有protocolhost 和才认为是相同的strong> port 相同:同源策略

It is only considered to be the same if the protocol, host and port is the same: Same Origin Policy

如果你想启用它,你必须遵循 跨域资源共享 (cors)通过添加标题.Mozilla 有 示例

If you want to enable it you must follow Cross-Origin Resource Sharing (cors) by adding headers. Mozilla has examples

您需要在响应中添加 Access-Control-Allow-Origin 作为标题.允许所有人(您可能这样做):

You need to add Access-Control-Allow-Origin as a header in your response. To allow everyone (you should probably NOT do that):

Access-Control-Allow-Origin: *

如果您需要支持多个来源(例如 example.comwww.example.com),请设置 Access-Control-Allow-Origin 在您对请求中 Origin-header 的值的回复中(在您验证 Origin 已列入白名单之后.)

If you need to support multiple origins (for example both example.com and www.example.com), set the Access-Control-Allow-Origin in your reply to the value of the Origin-header from the request (after you verified that the Origin is white-listed.)

另外请注意,有些请求会发送一个带有 OPTION 方法的预检请求,因此如果您编写自己的代码,您也必须处理这些请求.请参阅 Mozilla 了解 示例.

Also note that some requests send a preflight-request, with an OPTION-method, so if you write your own code you must handle those requests too. See Mozilla for examples.

这篇关于同一域上的CORS错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455