简体   繁体   English

如何在React Native中使用Rest Api执行具有完整验证的登录

[英]how to perform login with complete validation using Rest Api in React native

I am trying to Build an Application in React native i have to perform Login User Authentication using Rest Api . 我正在尝试在React native中构建应用程序,我必须使用Rest Api执行登录用户身份验证。 Because i am new to React native i am not able to understand how to perform this action can any body help. 因为我是React Native的新手,所以我无法理解如何执行此操作,这对任何机构都是有帮助的。 Thanks 谢谢

React Native provides the Fetch API for your networking needs. React Native提供了Fetch API来满足您的网络需求。 Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. 如果您以前使用过XMLHttpRequest或其他网络API,则提取似乎很熟悉。 You may refer to MDN's guide on Using Fetch for additional information. 您可以参考MDN关于使用提取的指南以获取更多信息。

Making requests 发出请求

In order to fetch content from an arbitrary URL, just pass the URL to fetch: 为了从任意URL获取内容,只需将URL传递给fetch:

fetch('https://mywebsite.com/mydata.json');

example.js example.js

// Example POST method implementation:

postData('http://example.com/answer', {answer: 42})
  .then(data => console.log(JSON.stringify(data))) // JSON-string from `response.json()` call
  .catch(error => console.error(error));

function postData(url = '', data = {}) {
  // Default options are marked with *
    return fetch(url, {
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, cors, *same-origin
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, *same-origin, omit
        headers: {
            'Content-Type': 'application/json',
            // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        redirect: 'follow', // manual, *follow, error
        referrer: 'no-referrer', // no-referrer, *client
        body: JSON.stringify(data), // body data type must match "Content-Type" header
    })
    .then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.error('Error:', error));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM