Vue 项目中遇到的跨域问题及解决方法(后台php)

作者:sansan_7957 时间:2023-11-16 09:14:13 

问题描述

前端 vue 框架,后台 php,百度跨域问题后台加这段代码


header("Access-Control-Allow-Origin: *");

加了之后报这个错:

The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

Vue 项目中遇到的跨域问题及解决方法(后台php)

解决办法

文章链接:CORS: credentials mode is ‘include'


xhrFields: {
withCredentials: false
},

withCredentials: true 改成 withCredentials: false,如果你没加上面那段代码当然也不会报这个错。虽然是解决方法很简单,但经此发现许多知识没掌握不得不梳理下。

•HTTP 请求方式有许多种,有些请求会触发 CORS 预检请求。“需预检的请求”会使用 OPTIONS 方法发起一个预检请求到服务器,以获知服务器是否允许该实际请求。

•对于跨域请求浏览器一般不会发送身份凭证信息。如果要发送凭证信息,需要设置 XMLHttpRequest 的 withCredentials 属性为 true:withCredentials: true。此时要求服务器的响应信息中携带 Access-Control-Allow-Credentials: true,否则响应内容将不会返回。

•对于携带身份凭证的请求,服务器不得设置 Access-Control-Allow-Origin 的值为“*”。因为请求头携带了 Cookie 信息。要将 Access-Control-Allow-Origin 的值设置为 http://www.zrt.local:8080。

•另外,响应头中也携带了 Set-Cookie 字段,尝试对 Cookie 进行修改。如果操作失败,将会抛出异常。

跨域请求想要带上 cookies 必须在请求头里面加上:


crossDomain: true,
xhrFields: {
 withCredentials: true
}

又变成文章开头的问题了,解决办法:

后台代码:


Access-Control-Allow-Origin: 'http://www.zrt.local:8080'
Access-Control-Allow-Credentials: true

前端代码:


crossDomain: true,
xhrFields: {
 withCredentials: true
}

跟之前一样就行了。

总结

以上所述是小编给大家介绍的Vue 项目中遇到的跨域问题及解决方法,希望对大家有所帮助.

来源:https://blog.csdn.net/sansan_7957/article/details/79714486

标签:vue,跨域,问题
0
投稿

猜你喜欢

  • Python+selenium破解拼图验证码的脚本

    2023-11-22 05:24:05
  • 在PyCharm中批量查找及替换的方法

    2023-05-29 17:15:13
  • 20行Python代码实现一款永久免费PDF编辑工具

    2023-11-17 23:51:45
  • driver = webdriver.Chrome()报错问题及解决

    2023-04-09 21:03:16
  • tensorflow多维张量计算实例

    2022-02-05 00:43:48
  • 简单分析python的类变量、实例变量

    2022-12-20 20:39:56
  • js循环改变div颜色具体方法

    2024-04-19 10:29:45
  • SQL Server数据库备份多种方法

    2008-11-24 15:27:00
  • 深入了解Golang官方container/heap用法

    2024-05-13 10:44:42
  • Python实现k-means算法

    2023-07-01 22:06:53
  • Python MySQLdb 执行sql语句时的参数传递方式

    2024-01-28 08:45:33
  • python被修饰的函数消失问题解决(基于wraps函数)

    2021-07-22 17:32:34
  • python爬虫爬取快手视频多线程下载功能

    2021-10-29 09:24:04
  • Go 热加载之fresh详解

    2024-03-23 14:27:26
  • python 函数定位参数+关键字参数+inspect模块

    2023-07-05 23:04:37
  • Oracle临时表空间删除和重建实现过程

    2023-07-18 09:26:04
  • Python实现程序的单一实例用法分析

    2023-01-08 11:38:14
  • 基于FME使用Python过程图解

    2023-12-17 07:57:37
  • python学习实操案例(五)

    2023-08-22 01:52:43
  • go micro微服务proto开发安装及使用规则

    2024-03-19 14:40:13
  • asp之家 网络编程 m.aspxhome.com