<i id='1P59H'><tr id='1P59H'><dt id='1P59H'><q id='1P59H'><span id='1P59H'><b id='1P59H'><form id='1P59H'><ins id='1P59H'></ins><ul id='1P59H'></ul><sub id='1P59H'></sub></form><legend id='1P59H'></legend><bdo id='1P59H'><pre id='1P59H'><center id='1P59H'></center></pre></bdo></b><th id='1P59H'></th></span></q></dt></tr></i><div id='1P59H'><tfoot id='1P59H'></tfoot><dl id='1P59H'><fieldset id='1P59H'></fieldset></dl></div>
  1. <small id='1P59H'></small><noframes id='1P59H'>

      <bdo id='1P59H'></bdo><ul id='1P59H'></ul>
  2. <tfoot id='1P59H'></tfoot>

      <legend id='1P59H'><style id='1P59H'><dir id='1P59H'><q id='1P59H'></q></dir></style></legend>

      fetch POST 返回 HTTP 415,而 curl 继续正常并返回结果

      时间:2023-10-02
        <i id='2jtap'><tr id='2jtap'><dt id='2jtap'><q id='2jtap'><span id='2jtap'><b id='2jtap'><form id='2jtap'><ins id='2jtap'></ins><ul id='2jtap'></ul><sub id='2jtap'></sub></form><legend id='2jtap'></legend><bdo id='2jtap'><pre id='2jtap'><center id='2jtap'></center></pre></bdo></b><th id='2jtap'></th></span></q></dt></tr></i><div id='2jtap'><tfoot id='2jtap'></tfoot><dl id='2jtap'><fieldset id='2jtap'></fieldset></dl></div>

        <tfoot id='2jtap'></tfoot>
      1. <legend id='2jtap'><style id='2jtap'><dir id='2jtap'><q id='2jtap'></q></dir></style></legend>
        • <bdo id='2jtap'></bdo><ul id='2jtap'></ul>

              <tbody id='2jtap'></tbody>

              <small id='2jtap'></small><noframes id='2jtap'>

              • 本文介绍了fetch POST 返回 HTTP 415,而 curl 继续正常并返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这就是我的代码的样子

                let body = {
                            authCode: "XXXX",
                            clientId: "YYYYYY",
                            clientSecret: "ZZZZZZ"
                        };
                
                
                        fetch('https://api.myapp.com/oauth/token',{
                            method: "POST",
                            headers: {
                                "Content-Type": "application/json",
                                "Accept": "application/json"
                            },
                            mode: 'no-cors',
                            body: body
                        }).then(function(response){
                            console.log("response: ", response);
                        }).catch(function(error){
                            console.log("could not get tokens: ", error);
                        })
                

                在 Chrome 中,这就是我看到的

                In Chrome, this is what I see

                我尝试通过 curl 命令 做到这一点,这就是它的样子

                I tried to do this by curl command and this is what it looks like

                  ~ curl -X POST -H "Content-Type: application/json" -d '{
                  "authCode": "XXXX",
                  "clientId": "YYYYY",
                  "clientSecret": "ZZZZZZZZ"
                }' https://api.myapp.com/oauth/token
                {"authToken":"e141kjhkwr3432ca9b3d2128385ad91db4cd2:cca6b151-cab4-4de2-81db-9a739a62ae88:23000000"}
                

                我在这里做错了什么?

                更新

                改成following后,结果还是HTTP 415

                After changing it to following, the result is still HTTP 415

                fetch('https://api.myapp.com/oauth/token',{
                            method: "POST",
                            headers: {
                                "Content-Type": "application/json",
                                "Accept": "application/json"
                            },
                            mode: 'no-cors',
                            body: JSON.stringify(body)
                        }).then(function(response){
                            console.log("response: ", response);
                        }).catch(function(error){
                            console.log("could not get tokens: ", error);
                        })
                

                有趣的是,我意识到我发送了标题 "Content-Type": "application/json" 而我得到的是 content-type: text/plain,为什么会这样?

                Interestingly, I realized that I sent the header "Content-Type": "application/json" while what I get back is content-type: text/plain, why that might be happening?

                推荐答案

                你必须像这样定义 AcceptContent-Type 标头并字符串化你的数据对象

                you must define Accept and Content-Type headers like that and stringify your data object

                const params = {
                            headers: {
                                'Accept': "application/json, text/plain, */*",
                                'Content-Type': "application/json;charset=utf-8"
                            },
                            body: JSON.stringify(yourObject),
                            method: "POST"
                        };
                
                fetch(url, params)
                        .then(data => { console.log('data', data)})
                        .then(res => { console.log('res', res) })
                        .catch(error => { console.log('error', error) });
                

                这篇关于fetch POST 返回 HTTP 415,而 curl 继续正常并返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:react native 中的并行获取请求 下一篇:react-native fetch 返回奇怪的响应

                相关文章

              • <tfoot id='sIgtW'></tfoot>
                      <bdo id='sIgtW'></bdo><ul id='sIgtW'></ul>
                    <i id='sIgtW'><tr id='sIgtW'><dt id='sIgtW'><q id='sIgtW'><span id='sIgtW'><b id='sIgtW'><form id='sIgtW'><ins id='sIgtW'></ins><ul id='sIgtW'></ul><sub id='sIgtW'></sub></form><legend id='sIgtW'></legend><bdo id='sIgtW'><pre id='sIgtW'><center id='sIgtW'></center></pre></bdo></b><th id='sIgtW'></th></span></q></dt></tr></i><div id='sIgtW'><tfoot id='sIgtW'></tfoot><dl id='sIgtW'><fieldset id='sIgtW'></fieldset></dl></div>
                  1. <legend id='sIgtW'><style id='sIgtW'><dir id='sIgtW'><q id='sIgtW'></q></dir></style></legend>
                  2. <small id='sIgtW'></small><noframes id='sIgtW'>