Javascript 如何传递头+;使用Vue JS axios的POST api中的主体
我试图调用aws Cognito()的post API。它在我的邮递员客户身上非常有效。但是我在VueJS代码中遇到了这个问题 下面是我的代码片段 测试.vueJavascript 如何传递头+;使用Vue JS axios的POST api中的主体,javascript,vue.js,axios,aws-cognito,quasar-framework,Javascript,Vue.js,Axios,Aws Cognito,Quasar Framework,我试图调用aws Cognito()的post API。它在我的邮递员客户身上非常有效。但是我在VueJS代码中遇到了这个问题 下面是我的代码片段 测试.vue <script> HTTP.post(`token`, { 'grant_type': 'authorization_code', 'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXX', 'redirect_uri': 'http://localhost:8080/call
<script>
HTTP.post(`token`, {
'grant_type': 'authorization_code',
'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'redirect_uri': 'http://localhost:8080/callback',
'code': this.$route.query.code
})
.then(response => {
console.log('Response: ' + response)
})
.catch(e => {
console.log('Error: ' + e)
})
</script>
我不确定,但问题是在我的邮递员中,我在body+标题中使用了“application/x-www-form-urlencoded”选项。在这里,我不能在体内设置这个值
我发现我的标题和正文中的“application/x-www-form-urlencoded”选项设置不正确
我尝试了{emulateJSON:true}选项。但不起作用
我得到了HTTP代码:400
{“data”:{“error”:“invalid_request”},“status”:400,“statusText”:“Bad request”,“headers”:{“pragma”:“no cache”,“content type”:“application/json;charset=UTF-8”,“cache control”:“no cache,no store,max age=0,必须重新验证”,“expires”:“0”},“config”:{“transformRequest”:{},“transformResponse”:{},“timeout”:0,“xsrfCookieName”:“XSRF-TOKEN”,“xsrfHeaderName”:“X-XSRF-TOKEN”,“maxContentLength”:-1,“headers”:{“Accept”:“application/json”,“Content Type”:“application/X-www-form-urlencoded”},“method”:“post”,“baseURL”:“url”:“url”:“data”:“{”grant\u Type\“:“authorization\u code\”,“client\u id\”:“4jcmshlse80ab667okni41fbf5\”,“redirect\u uri\”:“,“code\”:“e19170dc-3DF-420e-997B13”,“请求”:{}
唯一接受应用程序/x-www-form-urlencoded
并且您正在发送JSON(因为axios只将JavaScript对象序列化为JSON)
如何使用axios发送应用程序/x-www-form-urlencoded
:
下面是qs
库的示例
<script>
HTTP.post(`token`, qs.stringify({
'grant_type': 'authorization_code',
'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'redirect_uri': 'http://localhost:8080/callback',
'code': this.$route.query.code
}))
.then(response => {
console.log('Response: ' + response)
})
.catch(e => {
console.log('Error: ' + e)
})
</script>
HTTP.post(`token`,qs.stringify)({
“授权类型”:“授权代码”,
“客户id”:“XXXXXXXXXXXXXXXXXXXXXXXXX”,
“重定向uri”:”http://localhost:8080/callback',
“代码”:此。$route.query.code
}))
。然后(响应=>{
console.log('响应:'+响应)
})
.catch(e=>{
console.log('错误:'+e)
})
唯一接受应用程序/x-www-form-urlencoded
并且您正在发送JSON(因为axios只将JavaScript对象序列化为JSON)
如何使用axios发送应用程序/x-www-form-urlencoded
:
下面是qs
库的示例
<script>
HTTP.post(`token`, qs.stringify({
'grant_type': 'authorization_code',
'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'redirect_uri': 'http://localhost:8080/callback',
'code': this.$route.query.code
}))
.then(response => {
console.log('Response: ' + response)
})
.catch(e => {
console.log('Error: ' + e)
})
</script>
HTTP.post(`token`,qs.stringify)({
“授权类型”:“授权代码”,
“客户id”:“XXXXXXXXXXXXXXXXXXXXXXXXX”,
“重定向uri”:”http://localhost:8080/callback',
“代码”:此。$route.query.code
}))
。然后(响应=>{
console.log('响应:'+响应)
})
.catch(e=>{
console.log('错误:'+e)
})
您需要使用简单的JSON.stringify
或类似于.trued!的库对负载进行字符串化,但问题仍然相同。您需要使用简单的JSON.stringify
或类似于.trued!的库对负载进行字符串化,但问题仍然相同。