我无法在角度js $ http.post中获得响应

时间:2022-02-10 13:39:41

I want to check whether the email id already exists in the db. If the email id exists, it should return false, else true. I'm not able to get the response. I've attached the controller code and the php code. [the variable validUser is undefined]

我想检查数据库中是否已存在电子邮件ID。如果电子邮件ID存在,则应返回false,否则为true。我无法得到答复。我已经附加了控制器代码和PHP代码。 [变量validUser未定义]

Controller.js

signUpApp.controller("signUpCtrl",function($scope,$http){
        $scope.register = function(form,user){
           if (form.$valid)
           {
             $http.post("http://localhost/checkUser.php?email="+user.email)
                  .then(function(response){
                      validUser=response;
                  });
             if(validUser=="true")
             {
               alert("valid user");
             }
             else
             {
               alert("already exists");
             }

           }
        }
     });

checkUser.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("localhost", "root", "", "user_details");
//$data = json_decode(file_get_contents("php://input"));
//$email = mysql_real_escape_string($data->email);
$email = $_POST['email'];
$result = $conn->query("SELECT count(*) as count from user where email='$email'");

$outp = "";
$rs = $result->fetch_array(MYSQLI_ASSOC)

    if ($rs['count']==0)
    {
        $outp ="true";
    }
    else
    {
        $outp ="false";
    }
$conn->close();

echo($outp);
?>

2 个解决方案

#1


1  

You're not checking the response in the correct place, or rather - at the correct time.

您没有在正确的位置检查响应,或者更确切地说 - 在正确的时间检查响应。

$http.post returns immediately. Your .then callback is called when the response is returned from the server. The code after the call to post (your if statements) is executed right after $http.post returns, and before the response is received from the server.

$ http.post立即返回。从服务器返回响应时,将调用.then回调。调用post(你的if语句)之后的代码在$ http.post返回之后,在从服务器收到响应之前执行。

You should place your validation code inside your callback:

您应该将验证代码放在回调中:

$http.post(...).then(function(response) {
    validUser = response;
    if(validUser==="true") { 
         ...
    } else if (validUser==="false") {
         ...
    }
}

#2


0  

You're if statement needs to be inside the .then callback, otherwise you'll end up checking it before youre ajax request gets responded to

你是if语句需要在.then回调中,否则你最终会在ajax请求被响应之前检查它

    signUpApp.controller("signUpCtrl",function($scope,$http){
    $scope.register = function(form,user){
       if (form.$valid)
       {
         $http.post("http://localhost/checkUser.php?email="+user.email)
              .then(function(response){
                  validUser=response;
                  if(validUser=="true")
                  {
                    alert("valid user");
                  }
                  else
                  {
                    alert("already exists");
                  }
              });
       }
    }
 });

#1


1  

You're not checking the response in the correct place, or rather - at the correct time.

您没有在正确的位置检查响应,或者更确切地说 - 在正确的时间检查响应。

$http.post returns immediately. Your .then callback is called when the response is returned from the server. The code after the call to post (your if statements) is executed right after $http.post returns, and before the response is received from the server.

$ http.post立即返回。从服务器返回响应时,将调用.then回调。调用post(你的if语句)之后的代码在$ http.post返回之后,在从服务器收到响应之前执行。

You should place your validation code inside your callback:

您应该将验证代码放在回调中:

$http.post(...).then(function(response) {
    validUser = response;
    if(validUser==="true") { 
         ...
    } else if (validUser==="false") {
         ...
    }
}

#2


0  

You're if statement needs to be inside the .then callback, otherwise you'll end up checking it before youre ajax request gets responded to

你是if语句需要在.then回调中,否则你最终会在ajax请求被响应之前检查它

    signUpApp.controller("signUpCtrl",function($scope,$http){
    $scope.register = function(form,user){
       if (form.$valid)
       {
         $http.post("http://localhost/checkUser.php?email="+user.email)
              .then(function(response){
                  validUser=response;
                  if(validUser=="true")
                  {
                    alert("valid user");
                  }
                  else
                  {
                    alert("already exists");
                  }
              });
       }
    }
 });