AngularJS $http.get() 不起作用


AngularJS $http.get() not working

我正在使用ZF2和AngularJS来创建测验应用程序。当我运行代码时,没有发生错误,也没有结果。

(function(angular) {
function MainController($scope,$http) {
    $scope.question = function(id)
    {
        var site = "http://localhost/zf/public/interviewer";
        var page = "/jsonquestion/"+id;
        alert(site + page);
        var reqQuestion = $http.get(site + page);
        reqQuestion.success(function(data, status, headers, config) {$scope.question.questions = data;});
        reqQuestion.error(function(data, status, headers, config){alert("AJAX failed!");});
        alert(data);
    }
};
angular.module("app", []).controller("MainController", ["$scope", MainController]);

})(棱角);

我的 zend 部分是

public function jsonquestionAction()
 {
   $id = (int) $this->params()->fromRoute('id', 0);
   $questions = $this->getQuestionsTable()->getQuestions($id);
   $result = json_encode($questions);
   $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
   fwrite($myfile, $result);
   fclose($myfile);
   echo $result;
   return $result;
 }

当我从浏览器调用http://localhost/zf/public/interviewer/jsonquestion/1时,它的工作并返回 Json

您没有向控制器注入$http尝试更改为此内容。

angular.module("app", []).controller("MainController", ["$scope", "$http", MainController]);

在黑暗中拍摄。 你的 zend 函数叫做 jsonquestionAction,但你的页面调用是针对 jsonquestion 的。

也许改变这个:

 var page = "/jsonquestion/"+id;

对此:

var page = "/jsonquestionAction/"+id;

会有所帮助。