how to access localhost asp.net core web api from outside

412 Views Asked by At

I have react native app.i want get data from the asp.net core web API .I want to to access localhost asp.net core web API from outside(from react native app)

1

There are 1 best solutions below

0
On

You can use many 3rd party library for HTTP request such as axios, fetch even XMLHttpRequest as you want to access core web API. You can use the following approaches it doesn't matter what's your endpoint.

 fetch('https://jsonplaceholder.typicode.com/posts/1', {
         method: 'GET'
      })
      .then((response) => response.json())
      .then((responseJson) => {
         console.log(responseJson);
         this.setState({
            data: responseJson
         })
      })
      .catch((error) => {
         console.error(error);
      }); 

using XMLHttpRequest

var xhr = new XMLHttpRequest()
    xhr.addEventListener('load', () => {
      console.log(xhr.responseText)
    })
    xhr.open('GET', 'https://dog.ceo/api/breeds/list/all')
    xhr.send()

using axios

axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .finally(function () {
  });