在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ fetch請(qǐng)求出錯(cuò)

fetch請(qǐng)求出錯(cuò)

有兩個(gè)錯(cuò)誤
Uncaught (in promise) TypeError: Failed to fetch

Failed to load 'url鏈接' Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

這是代碼
url那里我原來放的是后臺(tái)給的地址

fetch('url',
{
 method: 'POST',
 headers: {
           'Accept': 'application/json',
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({"password": values.password})
}).then(res => res.json()).then(json => console.log(json));
回答
編輯回答
孤巷

把url 上面的兩個(gè)單引號(hào)去掉先

fetch('url', //**這個(gè)url字符串,不是變量,**
{
 method: 'POST',
 headers: {
           'Accept': 'application/json',
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({"password": values.password})
}).then(res => res.json()).then(json => console.log(json));
2017年9月20日 18:02
編輯回答
替身

因?yàn)槟?'Content-Type': 'application/json'這樣配置,所以觸發(fā)了跨域中的預(yù)檢請(qǐng)求。后端配置Access-Control-Allow-Headers不夠的,后端還需要單獨(dú)對(duì)預(yù)檢的option請(qǐng)求單獨(dú)處理.具體為什么,可以參考這篇文章

2018年2月19日 20:22
編輯回答
淚染裳

如果是開發(fā)測(cè)試的話可以考慮裝一個(gè):allow-control-allow-origin:*插件(chrome)
https://chrome.google.com/web...

如果后臺(tái)是nginx服務(wù)器的話 有可能需要在nginx中配置
比如:

add_header Access-Control-Allow-Origin $http_origin; 
add_header Access-Control-Allow-Credentials 'true';
add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS';
add_header Access-Control-Allow-Headers 'App-User-Agent, App-IDENTIFIER'; #自定義的header
2017年5月26日 06:14
編輯回答
尐潴豬

需要后端配置Access-Control-Allow-Headers
跨域了

2017年9月1日 13:17