Angular ajax 使用 $http 失败
Angular ajax failed using $http
我在使用 $http 和我的自定义服务时TypeError: Cannot read property 'success' of undefined
出错。
我的应用.js :
app.controller('AppCtrl', ['$scope', '$http', 'topicContent', function($scope, $http, topicContent){
topicContent.request().success(function(data){
$scope.threadContent = data;
console.log(data);
});
}]);
app.factory('topicContent', ['$http', function($http){
var query = function() {
return
$http({
url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
method: "GET"
});
}
return {
request : function(){
return query();
}
}
}]);
这太奇怪了,我找不到任何缺陷。
问题是这段代码
var query = function() {
return
$http({
url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
method: "GET"
});
}
相当于这个:
var query = function() {
return;
$http({
url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
method: "GET"
});
}
由于在JavaScript中自动插入分号。请注意,return
后有分号;
。因此,解决方案是在语句之后$http
移动到同一行return
:
return $http({ ... });
阅读有关在什么情况下 javascript 解释器自动插入分号;
的信息,例如这里。在您的情况下return
解释器将插入;
,这使query
函数返回undefined
。因此,您遇到的错误。
相关文章:
- 当我在chrome中切换设备模式时,$http.get请求失败
- 添加@javascript标记时,Cucumber中的HTTP身份验证失败
- Angular Cross Origin Request CORS失败,但节点http.get()成功返回
- postmates api的AJAX中的HTTP身份验证失败
- AngularJS$http返回200正常,但失败了
- $http-get方法即使使用AngularJS中.Net WebApi的有效json也会失败
- 处理 HTTP 请求失败
- 当我的HTTP呼叫因互联网访问而失败时,有没有办法发出错误警报
- 检查 http 响应代码以显示相应的失败消息
- 如何将 $http.put 封装到一个函数中,该函数执行某些操作,然后返回通过或失败的承诺
- CORS 在 http 服务器上失败
- 有没有办法阻止失败的HTTP请求在浏览器控制台中显示为错误
- 从javascript进行http调用时失败
- 如何检测所有 http GET 失败
- Angular ajax 使用 $http 失败
- 为什么 AngularJS 指令适用于硬编码值,但使用 http 请求/响应失败
- 捕获失败的同步 HTTP 请求
- AngularJS:存储失败的 http 请求并稍后再试
- Node JS HTTP Server request.on('data') 事件失败
- js到的json无法打开流:http请求失败