无法使用$http读取未定义的属性“成功”
Cannot read property 'success' of undefined using $http
在我的控制器中,我这样做
topicContent.request().success(function(data){
$scope.threadContent = data;
});
在我的工厂里,我写道:
app.factory('topicContent', ['$http', function($http){
return
var query = function() {
$http({
url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
method: "GET"
});
}
return {
request : function(){
return query();
}
}
}]);
我已经检查了我的服务没有依赖项错误。知道为什么它说成功是不确定的吗?
您不会从 query()
返回任何内容。尝试:
app.factory('topicContent', ['$http', function($http){
var query = function() {
// HERE!!
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"
});
}
删除第 2 行的返回语句:
app.factory('topicContent', ['$http', function($http){
return
也许以下内容会有所帮助:
在控制器中:
topicContent.request().then(function(data){
$scope.threadContent = data;
});
并在服务
app.factory('topicContent', ['$http', function($http){
var query = function() {
**return** $http({
url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
method: "GET"
}).then(function(response){
**return** response.data;
});
}
return {
request : query
}
}]);
请注意服务的查询函数中的 2 个 return 语句,以及服务返回的简单对象。
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 全局变量和全局对象的属性之间有什么区别吗
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 序列化数据属性中对象的最可靠方法
- 分析高度属性时出现意外值{{specs.height}}.index.html
- TypeError:无法读取属性'推'未定义的JavaScript
- 客户端验证 没有验证的属性上的成功消息
- AJAX/JS/PHP:如何替换发送到PHP文件的文件中的文本,并传递成功属性
- jQuery 控件的 AJAX 文本属性成功,返回 [对象对象]
- 成功属性无法在视图中触发
- 解析失败,出现: 类型错误: 无法读取未定义的属性“成功”
- 无法使用$http读取未定义的属性“成功”
- 无法读取 null 的属性“成功”
- 如何为窗体返回布尔值's”;onsubmit”;属性,这取决于ajax调用的成功与否
- 未捕获的类型错误:无法读取属性'成功'的未定义
- 可以't在promise成功后在ember中设置属性
- 在data-*属性中成功回调x可编辑编码
- 如何访问成功返回的jQuery jqxhr对象的属性
- Javascript:从ajax成功设置类属性