当前位置 : 主页 > 网页制作 > React >

reactjs – React – fetch post content-type在fiddler中查看时正在改变

来源:互联网 收集:自由互联 发布时间:2021-06-15
我有一个获取请求类型似乎正在改变,这是搞乱我的帖子.我提交我的基本表格(仅限一个字段).这是获取. handleSubmit(event, data) { //alert('A name was submitted: ' + this.state.value); event.preventDefault()
我有一个获取请求类型似乎正在改变,这是搞乱我的帖子.我提交我的基本表格(仅限一个字段).这是获取.

handleSubmit(event, data) {
    //alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
    console.log("SUBMIT STATE::", this.state.value);
    return (
        fetch("//localhost:5000/api/values/dui/", {
            method: "post",
            mode: 'no-cors',
            headers: {
                'Access-Control-Allow-Origin': '*',
                'Content-Type': 'application/json',
                'Accept': 'application/json',                  
            },
            body: JSON.stringify({
                name: this.state.value,
            })
        }).then(response => {
            if (response.status >= 400) {
                this.setState({
                    value: 'no greeting - status > 400'
                });
                throw new Error('no greeting - throw');
            }
            return response.text()
        }).then(data => {
            var myData = JSON.parse(data);
            this.setState({
                greeting: myData.name,
                path: myData.link
            });
        }).catch(() => {
            this.setState({
                value: 'no greeting - cb catch'
            })
        })
    );


}

但是当我在fiddler中看到这个内容时,类型现在是’content-type:text / plain; charset = UTF-8′.这是原始的提琴手:

POST http://localhost:5000/api/values/dui/ HTTP/1.1
Host: localhost:5000
Connection: keep-alive
Content-Length: 16
accept: application/json
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

content-type:text / plain; charset = UTF-8
   推荐人:http://localhost:3000/
   Accept-Encoding:gzip,deflate,br
   Accept-Language:en-US,en; q = 0.8

{"name":"molly"}

在DOM Inspector中,我看到:

POST http://localhost:5000/api/values/dui/ 415(不支持的媒体类型)

我也觉得很奇怪’接受’是小写以及’内容类型’.任何原因发生这种情况.我的搜索还没有找到任何具体内容.

当模式:为请求设置’no-cors’时,浏览器将不允许您设置除 CORS-safelisted request-headers之外的任何请求标头.请参阅 the spec requirements about adding headers:

To append a name/value (name/value) pair to a Headers object (headers), run these steps:

  1. Otherwise, if guard is “request-no-cors” and name/value is not a 07002, return.

在该算法中,return等于“返回而不将该头添加到Headers对象”.

而之所以将其设置为text / plain; charset = UTF-8是因为the algorithm for the request constructor调用了extract a body algorithm,其中包括以下步骤:

Switch on object’s type:

USVString

  • Set Content-Type to text/plain;charset=UTF-8.
网友评论