[英]axios in vue js send request as string , not object
how can i resolve this problem ?我该如何解决这个问题? this is main.js I want to send the name to the server via axios in vuejs But I can't get name from $_Request
这是 main.js 我想通过 vuejs 中的 axios 将名称发送到服务器但我无法从 $_Request 获取名称
import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
Vue.config.productionTip = false
Vue.prototype.$http = axios
new Vue({
render: h => h(App),
}).$mount('#app')
and this is my template script这是我的模板脚本
export default {
data() {
return {
header:{headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}},
name:null,
r:null,
}
},
methods: {
post:function(){
this.$http.post('http://127.0.0.1/saver.php',{
name:this.name,
},this.header).then(r => {
this.r=r;
// eslint-disable-next-line no-console
console.log(typeof {name:this.name})
}).catch(err => {
// eslint-disable-next-line no-console
console.log(err.response.data)
});
}
},
}
</script>
and this is my php server side code :这是我的 php 服务器端代码:
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_REQUEST))
{
print_r($_REQUEST);
}
?>
params :参数:
and this is response that returned from server :这是从服务器返回的响应:
As Axios documentation states,正如Axios 文档所述,
By default, axios serializes JavaScript objects to JSON.
默认情况下,axios 将 JavaScript 对象序列化为 JSON。 To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.
要改为以 application/x-www-form-urlencoded 格式发送数据,您可以使用以下选项之一。
<...>
<...>
In a browser, you can use the URLSearchParams API
在浏览器中,您可以使用 URLSearchParams API
<...>
<...>
Alternatively, you can encode data using the qs library
或者,您可以使用 qs 库对数据进行编码
It should be:它应该是:
this.$http.post('http://127.0.0.1/saver.php', qs.stringify({
name:this.name,
}),this.header)
Alternatively, server side can be modified to use JSON payload exclusively.或者,可以修改服务器端以专门使用 JSON 负载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.