Redux 获取正文不使用没有 CORS 模式

redux fetch body is not use with no cors mode

本文关键字:CORS 模式 获取 正文 Redux      更新时间:2023-09-26

>我有这个调用函数的动作:

dispatch(Api({url: "my_url", method: "POST", data: data}))

在这里,我将数组作为数据传递。.

import fetch from 'isomorphic-fetch'
export default function Api({url, method, headers, data}={}){
    return dispatch => {
        console.log(data)
        console.log(url)
        console.log(method)
        console.log(JSON.stringify(data))
        let response = fetch(url, {
            mode: 'no-cors',
            method: method || null,
            body: data || null, 
        }).then(function(response) {
            console.log("response");
             console.log(response)
            });
    }
}

在这里,我将fetchmode:'no-cors'一起使用,我想我正在传递所有参数。我这里的身体是我作为参数传递的简单数组。

当我看到响应时,它就像:

body: null
bodyUsed: false
headers: Headers
ok: false
status: 0
statusText: ""
type: "opaque"
url:""

在这里,我的身体没有被使用。

这里有什么问题?需要帮助

你得到的是一个opaque response [1] [2],因为你正在使用 fetch with mode: 'no-cors' .您需要使用 mode: 'cors',服务器需要发送所需的 CORS 标头 [3] 才能访问响应。