Using Ajax on React Native

Viewed 6047

I am working on react native with javascript, where I want to execute a python script. I want to use this code in there:

  return $.ajax({
    type: 'GET',
    url: 'test.py',

    success: function(response) {
      console.log(response);
    },
    error: function(response) {
      return console.error(response);
    }
  });

But I get the error that the code is unreachable. For some reason react native can't use ajax. Do you know how I use ajax correctly in react native?

2 Answers

You cannot use Jquery in react-native. Jquery needs HTML/CSS DOM to work upon but react-native has its own DOM implementation. It is a Native app.

You can use Axios or fetch. In fetch, you will need to handle error codes on your own. I recommend you to use Axios.

axios.get(url)
      .then(function (response) {
        console.log(response.data)
      })
      .catch(function (error) {
        if (error.response) // error from server
          console.log(error.response.data.message)
        else
          console.log("Error Occured. Please try Again.!") // error from app side
      });
  })
}

I recommend you to use Axios, or Fetch.

Your sample is based on Jquery, and I don't think that React Native uses Jquery.

Related