javascript - What can I use in a React app as an equivalent of Angular's $http.get - Stack Overflow

I am looking to make an http request within a React app and am wondering what the best method would be.

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
Add a ment  | 

2 Answers 2

Reset to default 8

checkout 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信