【问题标题】:http get request using angularjs使用angularjs的http获取请求
【发布时间】:2013-05-07 11:43:05
【问题描述】:

所以我想用 angularjs

发送Get request

但是我得到了这个错误

Error: Argument 'SimpleController' is not a function, got undefined

这是我的代码

var module = angular.module('myApp', []);

module.config(function ($httpProvider) {
    $httpProvider.defaults.headers.post['SOAPAction'] = 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems';
});

function SimpleController($scope,$http)
{
    $http({
        url: "http://localhost:8080/petstore/pets/list",
        method: "GET",
    })
    .success(function(data){
        console.log("SUCCESS");
        $scope.pets = data;
    }).error(function() {
        console.log("FAIL");
        console.log("data: "+data+"\nstatus: "+status+"\nheaders: "+headers+"\nconfig: "+config)
        $scope.pets = [ {"id":1,"name":"Angelfish","description":"Saltwater fish from Australia","category":"Fish","imageUrl":"fish1.jpg","price":10}];
    });
}

【问题讨论】:

  • 也请发<html/>,最好是jsfiddle.net
  • @Abdel-Rahman Shoman,我注意到您已经问了 4 个问题,但没有给任何人答案。您应该通过单击最能回答您的问题的答案旁边的复选标记来奖励答案,以便将来的访问者可以得到帮助。

标签: angularjs httprequest get-request


【解决方案1】:

代替

函数 SimpleController($scope,$http)...

尝试使用

module.controller('SimpleController',function($scope,$http){

// 函数体

});

【讨论】:

    【解决方案2】:

    看起来你有一个额外的逗号导致无效的功能:

    $http({
        url: "http://localhost:8080/petstore/pets/list",
        method: "GET",  // <--- remove that comma
    })
    

    【讨论】:

    • 嗯,是的,你是对的,但这并没有什么不同,仍然无法正常工作
    • 如果你得到一个未定义的错误,那是因为你有一些语法问题。您是否通过 jslint 运行过代码?
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 2016-01-29
    • 2018-05-31
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多