response是Response对象,包含Header、status、statusText等属性。要获得具体数据需要使用.json(用于JSON)、.text(用于文本)、.formData(用于FormData对象)等方法。至于为什么需要return,因为Response.json返回的是一个Promise,所以只能先return,再在下一层处理。fetch(url). then(function(response) { ...
.then(function(response) { // 操作成功完成,处理结果 console.log(response.data); }) .catch(function(error) { // 操作失败,处理错误 console.error(error); }); 在上面的示例中,我们使用axios库发送了一个GET请求到/api/data,并使用then函数注册了两个回调函数。第一个回调函数在操作成功完成后被调用,...
.then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); // 也可以通过 params 设置参数: axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(...
axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { // 这里可以继续return Promise console.log(response); }) .catch(function (error) { console.log(error); }) 优点 代码相对优雅。一定层度上解决了回调地狱的问题,面对较复杂的回调可以采用Promise的then链来解决 缺点 ...
$.ajax({ url: "example.com/api", type: "GET", data: { id: 1 } }).then(function(response) { // 处理成功后的操作 console.log("请求成功"); console.log(response); }).catch(function(error) { // 处理请求失败的操作 console.log("请求失败"); console.log(error); }); 在上述代码...
console.log(response.data); }) .catch(function(error) { // 处理请求失败的结果 console.log(error); }); 在上面的代码中,axios.get方法返回一个Promise对象。我们可以使用then方法来注册一个回调函数,当Promise对象成功解析时,该回调函数将被调用。在这个回调函数中,我们可以处理请求成功的结果。
getDataFunc() { let that = this; axios.get('/api/test.php') .then(function(response) { console.log(response); }) .catch(function(error) { console.log(error); }); }, Network里面看响应码是200,且返回了json数据,但是没有执行then直接执行了catch,且error还是undefined。———想请问是什么...
console.log('Send request ...'); axios.get('http://localhost:8080/api/hello/') .then(function (response) { console.log(response); return response.data; }) .catch(function (error) { console.log(error); }); } render() { console.log('[Page 1] render called') ...
axios.get('.then(function(response){returnnewPromise(function(resolve,reject){// 在一个新的线程中执行耗时操作setTimeout(function(){// 执行一些耗时的操作,如计算或者循环等for(vari=0;i<1000000;i++){// do something}resolve(response.data);},0);});}).then(function(data){console.log(data)...
console.log(response); }) .catch(function (error) { // handle error console.log(error); }) .then(function () { // always executed }); 这是我从axios 文档那边拿过来的 希望对你有帮助 失败的话应该会跳到catch那边 我自己这边验证是可以的 代码如下: 加了catch 去处理失败情况 效果如下: 操...