Ale*_*bes 95 javascript ajax function angularjs
我有这个代码:
app.controller('MainCtrl', function ($scope, $http){
$http.get('api/url-api')
.success(function (data, status, headers, config){
}
}
Run Code Online (Sandbox Code Playgroud)
在我当地的环境中,工作正常,但在服务器中,返回此错误:
TypeError:$ http.get(...).success不是函数
有任何想法吗?谢谢
Mih*_*nut 194
该.success语法是正确的高达角1.4.3.
对于Angular v.1.6之前的版本,您必须使用then方法.该then()方法有两个参数:a success和一个error将使用响应对象调用的回调.
使用该then()方法,将callback函数附加到返回的promise.
像这样的东西:
app.controller('MainCtrl', function ($scope, $http){
$http({
method: 'GET',
url: 'api/url-api'
}).then(function (response){
},function (error){
});
}
Run Code Online (Sandbox Code Playgroud)
Shortcut 方法也可用.
$http.get('api/url-api').then(successCallback, errorCallback);
function successCallback(response){
//success code
}
function errorCallback(error){
//error code
}
Run Code Online (Sandbox Code Playgroud)
您从响应中获得的数据应采用JSON格式.
JSON是一种传输数据的好方法,它在AngularJS中很容易使用
2之间的主要区别在于,.then()调用返回a promise(使用从a返回的值解析callback),而.success()更传统的注册方式是callbacks不返回a promise.
这可能是多余的,但上面最多的投票回答说.then(function (success),从Angular版本开始,这对我不起作用1.5.8.而是使用response然后在块内response.data获取我正在寻找的我的json数据.
$http({
method: 'get',
url: 'data/data.json'
}).then(function (response) {
console.log(response, 'res');
data = response.data;
},function (error){
console.log(error, 'can not get data.');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
112247 次 |
| 最近记录: |