【问题标题】:redux fetch body is not use with no cors moderedux fetch body 在没有 cors 模式的情况下不使用
【发布时间】:2016-05-12 05:13:41
【问题描述】:

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

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:""

这里我的身体没有被使用..

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

【问题讨论】:

  • 你有答案吗?我也面临类似的问题...
  • 是的,问题出在服务器端..您应该允许所有来源获取
  • 是的,这对我有帮助。我在 web 配置文件中添加了 '。但是我已经改为 axios 而不是 'isomorphic-fetch'

标签: javascript reactjs fetch redux fetch-api


【解决方案1】:

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

【讨论】:

  • 我已对 mode:'cors' 进行了必要的更改,如 asp.net/web-api/overview/security/… 中所述。但得到响应为 (...) bodyUsed:false headers:Headers ok:true status:200 statusText:"OK" type:"cors" url:.你能帮我看看数据吗?
  • 你有我可以测试的服务的公共 URL 吗?
  • 抱歉,目前 api/url 没有托管在任何地方。
猜你喜欢
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 2019-09-01
  • 2016-05-10
  • 1970-01-01
  • 2019-02-25
  • 2017-09-07
  • 2014-07-09
相关资源
最近更新 更多