在 React Native v0.63.2 中获取 unsupported_grant_type

fetch unsupported_grant_type in React Native v0.63.2

我在 Postman 中请求令牌成功,但在 React Native 项目中 {"error":"unsupported_grant_type"}

My Code i'm copy from 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

I solved my problem! Hope can help everyone

body:urlencoded.toString()

我遇到了同样的问题并通过以下方式解决了它:

  1. 将URLHTTP更改为HTTPS

  2. 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>