我可以在React应用程序中使用什么作为Angular的等价物;s$http.get
What can I use in a React app as an equivalent of Angular's $http.get
我想在React应用程序中发出http请求,我想知道最好的方法是什么。我知道在Angular中你可以使用$http.get
,所以类似于:
$http.get('API').success(function(data) {...
但是,在React应用程序中,我可以使用什么来达到同样的目的。像Axios这样的东西能用吗?
原谅我的无知,这里的新手。
检出window.fetch
。它有一个非常好用的API。它是一个fetch函数,现在在全局窗口范围内提供。
如果您想在不实现fetch
的情况下支持浏览器。试试github的这种polyfillhttps://github.com/github/fetch
这里有一个的快速示例
// 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 :(
})
这里有一个伟大的教程
这是的规格
您可以使用JQuery
库的ajax方法或Superagent
等特定库。对于Superagent
,只需将其与npm
一起安装,例如npm i superagent --save
,并发出http请求,例如:
request
.get('/search')
.end(function(err, res){
// function body
});
以下是完整的文档。
相关文章:
- Angular JS IE9 Hashbang url rewriting
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 使用angular重定向到html页面
- angular.js没有'无法在PhoneGap中处理视图标记
- Javascript(Angular)从一个对象数组到第二个数组查找值
- angular 1.5应用程序中的导航栏
- angular的下拉菜单
- Angular只从数组中获取所需的数据
- 如何将不可变的js导入angular 2(alpha)
- Angular js-返回一个包含类似
- 如何使用 Angular JS 将数据保存在数据库中
- 如何制作简单的php'在Javascript中的foreach等价物
- 将JSON对象传递给angular指令
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- Angular:更新一次性绑定的数据
- 我可以在React应用程序中使用什么作为Angular的等价物;s$http.get
- 淘汰赛's在Angular中的纯计算等价物
- 角的等价物是什么?angular 2中的isFunction