使用模式no-cors强制Content-Type到application / json

问题描述 投票:0回答:1

我想强制内容类型为application / json,但我有一个模式“no-cors”,实际上内容类型的返回是:text / plain; charset = UTF-8当我传递头文件时它是一样的我不知道怎么做。

我已经尝试使用fetch或Axios,URL在HTTP中,我在本地工作,所以可能是问题?我试过邮差,这很棒。

     fetch('http://link', {
            method: 'POST',
            credentials: 'include',
            redirect: 'follow',
            mode: 'no-cors',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                "cluster_id" : [],
                "surname" :["albert"],
                "gender" :[],

            }),
        }).then((result) => {
            this.setState({ allClient: result.data }, () => {
                this.setState({ load: true })
                console.log(this.state.allClient)
        })

所以,我想要内容类型:application / json而不是Content-Type:text / plain; charset = UTF-8

reactjs post content-type
1个回答
0
投票

您已经提到了CORS和localhost,但我不知道您的问题是基于所提供的信息。

content-type标头指定标头后面的主体的MIME类型。对于您的请求,它是请求正文的类型。对于响应,它是响应主体的类型。

您正确设置了请求的内容类型,并通过accept标头告诉服务器您正在查找的内容,但最终由服务器端设置标头。如果您控制服务器,您可以根据需要进行调整,但否则您将无法使用正在服务的内容。

© www.soinside.com 2019 - 2024. All rights reserved.