Content-Type設置為json就報錯cors

Content-Type設置為json就報錯cors

老師,這章的content-type設置為application/x-www-form-urlencoded可以成功拿到response,但是設置成json格式就會被瀏覽器報錯cors,是慕課網的接口的設置問題嗎


<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

</head>

<body>

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<script>

// console.log(axios);

const url = "https://www.imooc.com/api/http/search/suggest?words=js";


const p = axios(url,{

method: "post",

headers: {

// "Content-Type": "application/x-www-form-urlencoded"

"Content-Type": "application/json"

},

params: {

username: "alex",

age:18

},

data: {

gender: "male",

}

// data:"gender=male",

})

.then((response) => {

console.log(response);

})

.catch((error) => {

console.log(error);

})

</script>

</body>

</html>

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同學妳好,確實是接口設置問題,該接口沒有對Content-Type屬性值爲application/json的請求設置允許跨域,同學了解下即可,實際開發中遇到這種情況,可以跟後台人員溝通讓其處理下接口。


祝學習愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师