[å为ä¸é¢ä¸¤ç§æ åµ]
1. å¦ææå¡å¨æ¯æ CORS, åå¨å®¢æ·ç«¯è®¾ç½®ç¸åºç `Access-Control-Allow-Origin` å³å¯å¾å°æ°æ®ã
let myHeaders = new Headers({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/plain'
});
fetch(url, {
method: 'GET',
headers: myHeaders,
mode: 'cors'
}) .then((res) => {
// TODO
})
æå¡ç«¯æ¯å¦æ¯æå¯ä»¥é®ä¸å端åäºï¼å¦ææ¯èªå·±æ¿æ å端ç¼ç ï¼åå¯ä»¥ç´æ¥èªå·±è®¾ç½®ï¼æ¯å¦å¦ææ¯ PHPer, header ä¸ä¸ååºå¤´å³å¯ã
header("Access-Control-Allow-Origin: *");
2. å¦ææå¡å¨ä¸æ¯æ CORSï¼ åä¸ç¨ä½¿ç¨ Fetch Api äºã
`Fetch Api` å¿ é¡»åå°æ¯æ `CORS`,ãå±ä»¬å¯ä»¥è¯ä¸ï¼å¦æä½ è®¾ç½®äº `{mode: ' cors '}`(ä¸è¬ç¨äºè¯·æ±API)ï¼å°±ä¼æ¥éåè¯ä½ ä½ è¯·æ±çæå¡å¨ä¸æ¯æ `CORS`ã大æ¦ä¼æ¥ä¸é¢çé误ï¼
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
å¦æ设置æ `{mode: ' no-cors '}` (ä¸è¬ç¨äºè¯·æ±å¾ççéæèµæº), è½ç¶ä¸ä¼æ¥éï¼ä½æ¯ç»æä¼ è¿å被æ è®°äºä¸º `opaque` çæ°æ®ï¼è¡¨æä½ æ²¡ææé访é®ã
è¿ç§æ åµä¸å¯ä»¥ä½¿ç¨ JSONPã