Le *_*Moi 2 javascript reactjs
我想在React应用程序中发出一个http请求,我想知道最好的方法是什么.我知道在Angular中你可以使用$http.get,所以类似于:
$http.get('API').success(function(data) {...
Run Code Online (Sandbox Code Playgroud)
但是我可以在React App中为同一目的使用什么.像Axios这样的东西会起作用吗?
请原谅我的无知,这里有新手.
小智 8
结帐window.fetch.它有一个非常好用的API.它是一个fetch函数,现在在全局窗口范围内提供.
如果你想支持浏览器而没有fetch实现.尝试从github https://github.com/github/fetch这样的polyfill
这是一个简单的例子
// 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 :(
})
Run Code Online (Sandbox Code Playgroud)
这是一个很棒的教程
这是规格
| 归档时间: |
|
| 查看次数: |
669 次 |
| 最近记录: |