Vue使⽤axios出现options请求⽅法以下代码如果需要在你本地跑起来:
Ⅰ.需要apache服务,并把php代码丢进去指定位置
1.从⼀段简易代码说起
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- 导⼊axios -->
<script src="unpkg/axios/dist/axios.min.js"></script>
</head>
<body>
<!-- POST 请求 -->
<form method="post">
<p>⽤户名:<input id="username" type="text" name="username" value="admin"> </p>
<br />
<p>密 码:<input id="password" type="text" name="password" value="123456"> </p>
<br />
<p><input id="btn" type="submit" value="登录" /></p>
</form>
</body>
<script>
//获取⽤户输⼊的登录信息
let username = ElementById('username').value;
let password = ElementById('password').value;
//不处理的数据对象
let data = {
username: username,
password: password
};
console.log(data);//{username: "admin", password: "123456"}
//阻⽌submit默认⾏为:表单提交刷新页⾯
event.preventDefault();
//提交请求获取响应数据
axios.post("st/day05/jiekou.php", data).then(res => {
console.log(res);
}).catch(err => {
console.log(err);
})
}
</script>
</html>
php接⼝代码
<?php
vue element admin// 制定允许其他域名访问
header("Access-Control-Allow-Origin:*");
// 允许的响应类型
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
// 响应头设置
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
// 获取form表单值
$username = $_POST['username'];
$password = $_POST['password'];
// 判断form表单中key
if(isset($_POST['username']) && isset($_POST['password'])){
// 判断username和password
if($username == "admin" && $password == "123456"){
$result = array("success" => 1, "code" => 101, "data" => array("username" => $username, "password" => $password));    }else{
$result = array("success" => 0, "code" => 103, "data" => null);
}
}else{
$result = array("success" => 0, "code" => 100, "data" => null);
}
// 将错误信息(数组)转换成json类型,返回前端
echo(json_encode($result));
>
结果:运⾏失败,⽆法获取响应数据
2.解决:
⽅法Ⅰ.引⼊qs模块处理数据:修改上⾯的html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- 导⼊axios -->
<script src="unpkg/axios/dist/axios.min.js"></script>
<!-- 导⼊qs -->
<script src="cdn.bootcss/qs/6.5.2/qs.min.js"></script>
</head>
<body>
<!-- POST 请求 -->
<form method="post">
<p>⽤户名:<input id="username" type="text" name="username" value="admin"> </p>
<br />
<p>密 码:<input id="password" type="text" name="password" value="123456"> </p>
<br />
<p><input id="btn" type="submit" value="登录" /></p>
</form>
</body>
<script>
//获取⽤户输⼊的登录信息
let username = ElementById('username').value;
let password = ElementById('password').value;
// ⽤qs处理数据对象
//qs的原理:就是将对象转变成字符串拼接到url上再发post请求
let data = Qs.stringify({
username: username,
password: password
});
console.log(data);//username=admin&password=123456
//阻⽌submit默认⾏为:表单提交刷新页⾯
event.preventDefault();
//提交请求获取响应数据
axios.post("st/day05/jiekou.php", data).then(res => {
console.log(res);
}).catch(err => {
console.log(err);
})
}
</script>
</html>
⽅法Ⅱ.后端开放options请求跨域,并⽤对应的⽅法获取options提交的复杂数据
<?php
// 制定允许其他域名访问
header("Access-Control-Allow-Origin:*");
// 响应类型
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
// 响应头设置
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
//如果是options请求,就结束执⾏下⾯语句
if($_SERVER['REQUEST_METHOD']=='OPTIONS'){
//exit是⽤来结束程序执⾏的,如果参数是字符串,PHP将会直接把字符串输出,
//如果参数是整型(范围是0-254),那个参数将会被作为结束状态使⽤。
exit('options类型的请求,结束');
}
//option请求⽆法⽤常规⽅法($_GET, $_POST, $_REQUEST)获取请求参数
$option_data_str = file_get_contents("php://input");
$option_data = json_decode($option_data_str,true);
$username = isset($option_data['username'])?$option_data['username']:"";
$password = isset($option_data['password'])?$option_data['password']:"";
// 判断form表单中key
if(isset($username) && isset($password)){
// 判断username和password
if($username == "admin" && $password == "123456"){
$result = array("success" => 1, "code" => 101, "data" => array("username" => $username, "password" => $password));    }else{
$result = array("success" => 0, "code" => 103, "data" => null);
}
}else{
$result = array("success" => 0, "code" => 100, "data" => null);
}
// 将错误信息(数组)转换成json类型,返回前端
echo(json_encode($result));
>
⼩结::
Ⅰ.个⼈是⽐较倾向于由后端来解决(前端还要引⼊qs插件,没什么必要)
Ⅱ.在vue⾥⾯使⽤qs的步骤
➀qs是⼀个npm仓库所管理的包,可通过npm install qs命令进⾏安装.
qs.parse()将URL解析成对象的形式
qs.stringify()将对象序列化成URL的形式,以&进⾏拼接(我们⼤都⽤到这个)
➁在全局main.js⾥引⼊qs并配置⽅法到原型上
import qs from 'qs';
Vue.prototype.$qs = qs;
在所有的vue实例组件⾥都可以直接⽤this.$qs.stringify(要处理的数据),进⾏数据转换
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。