I am looking to make an http request within a React app and am wondering what the best method would be.
I know in Angular you can use $http.get
, so something like:
$http.get('API').success(function(data) {...
But what can I use for the same purpose within a React App. Would something like Axios work?
Forgive my ignorance, total newbie here.
I am looking to make an http request within a React app and am wondering what the best method would be.
I know in Angular you can use $http.get
, so something like:
$http.get('API').success(function(data) {...
But what can I use for the same purpose within a React App. Would something like Axios work?
Forgive my ignorance, total newbie here.
Share Improve this question edited May 4, 2016 at 20:22 sehrob 1,04412 silver badges24 bronze badges asked May 4, 2016 at 19:56 Le MoiLe Moi 1,0253 gold badges16 silver badges41 bronze badges 2- Yes, axios is a viable alternative; we use it at my pany, and is also included in many of the React tutorials on egghead.io – lux Commented May 4, 2016 at 20:04
- There are a lot of technologies to choose from. I would suggest follow this page github./petehunt/react-howto And pick the one that suites you. – Khaled Garbaya Commented May 5, 2016 at 18:42
2 Answers
Reset to default 8checkout window.fetch
. It has a very nice API to work with. It is a fetch function is now provided in the global window scope.
if you want to support browser without fetch
implemented. try a polyfill like this one from github https://github./github/fetch
here is a quick example
// url (required), options (optional)
fetch('/some/url', {
method: 'get'
}).then(function(response) {
if (response.ok) {
return response.json()
}
}).then(function(json) {
console.log(json)
// set your state, your props, your child context do whatever you want
// with the json
// this.setState({ data: json })
}).catch(function(err) {
// Error :(
})
Here a great tutorial
Here is the spec
You can use JQuery
library's ajax methods or specific libraries like Superagent
. For Superagent
just install it with npm
, for example npm i superagent --save
and make http requests for example:
request
.get('/search')
.end(function(err, res){
// function body
});
Here is plete documentation for it.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745158950a4614294.html
评论列表(0条)