在React Native v0.63.2中获取unsupported_grant_type

4uqofj5v  于 2023-08-07  发布在  React
关注(0)|答案(2)|浏览(102)

我在Postman中成功请求Token,但在React Native项目中:
{“error”:“unsupported_grant_type”}
我的代码是从PostMan复制的:

var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/x-www-form-urlencoded");

    var urlencoded = new URLSearchParams();
    urlencoded.append("username", "hoanglongit96");
    urlencoded.append("password", "admin");
    urlencoded.append("grant_type", "password");

    var requestOptions = {
        method: 'POST',
        headers: myHeaders,
        body: urlencoded,
        redirect: 'follow'
    };

    fetch("http://192.168.1.38:7777/token", requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error));

字符串
测试结果:
{“error”:“unsupported_grant_type”}
我试着添加接受:application/json在header中

sbdsn5lh

sbdsn5lh1#

我解决了我的问题!希望能帮助大家

body:urlencoded.toString()

字符串

piv4azn7

piv4azn72#

我遇到了同样的问题,并通过以下方式解决了它:
1.将URLHTTP修改为HTTPS
1.在android/src/main/res/xml/network_security_config.xml中创建文件network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
   <network-security-config>
       <domain-config cleartextTrafficPermitted="true">
       <domain includeSubdomains="true">192.168.1.135</domain>
   </domain-config>
</network-security-config>

字符串

相关问题