dsc56927 2014-10-30 21:32
浏览 65
已采纳

AngularJS $ http.get无效。 没有检索数据

I'm new to AngularJS but I've already done a lot of things. I did a $http.post, that worked like a charm.

Heres the Angular code

$scope.login = function(user, pass) {
$scope.bIsVisible = false;

var dados = {"user": user, "pass": pass};
console.log(dados);

$http.post('http://base.corretoconcursos.com.br/cadernos/index.php/manageUsers/login', dados)
.success(function(data, status, headers, config) {
  if (data == false) 
  {
    $scope.isLogged = false;
    $scope.bIsVisible = true;
  } else {
    $scope.isLogged = true;
    $scope.userData = data;
    console.log(data);
  }
})
.error(function(data, status, headers, config) {
  alert('failed');
  $scope.bIsVisible = true;
});

And the manageUsers/login function

function login()
{
    $postdata = file_get_contents("php://input");
    $data = json_decode($postdata);

    $username = $data->user;
    $password = md5($data->pass);

    $datafrombase = $this->UsersModel->getUser(array(
        'user' => $username,
        'pass' => $password
        ));

    if ($datafrombase != null) {
        $user = array(
                    'user' => $datafrombase['user'],
                    'type' => $datafrombase['type'],
                    'logged_in' => true
                    );
        $this->session->set_userdata($user);
    }

    if ($datafrombase != null)
        print_r(json_encode($datafrombase));
    else
        return false;
}

Alright. It's working. I retrieve some data from database and OK. The real problem is when I do a $http.get and simply by doing a request on database or not, it doesn't send back the data that I want, when I do the console.log(data), it shows me an entirely HTML page, in fact, the one that is displaying. I'm getting a 200 status OK, but, a HTML page is coming. Don't know why.

Heres the Angular code

$scope.setLoggedOn = function(on) {

if (on) {

    $http.get('http://base.corretoconcursos.com.br/cadernos/index.php/manageUsers/retrieveLogin')
      .success(function(data, status) {
          console.log(data);
      })
      .error(function(data, status, headers, config) {
        alert('failed');
      });
}
else
  $scope.isLogged = false;
};

And heres the PHP code function I'm retrieving.

function retrieveLogin()
{
    $user = null;

    $user = array(
        'user' => $this->session->userdata('user'),
        'type' => $this->session->userdata('type'),
        'logged_in' => true
        );

    print_r(json_encode($user));
}

I'm stuck. I've even tried doing just a 'return true'; inside the php function, return 'string'; but nothing will work. What so wrong am I doing?

  • 写回答

2条回答 默认 最新

  • dongqian3198 2014-10-31 18:50
    关注

    Figured it out. If you're using CodeIgniter along sessions; and checking in the constructor if people are logged in to let them see that page or not; when you do the $http.get it will run the constructor and run that condition, and if they cannot see that page, even if you're just doing a request from that controller via AJAX, it won't let you make the request.

    I thought that the $http.get would only request the function (i-e, verifyUserInDatabase();), and give the data, but turns out, it doesn't.

    Thanks for the help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况