简体   繁体   中英

http_response_code() in php

I'm kind of new to php, I'm using angularJS on the front end that looks like this

app.controller('customersCtrl', function($scope, $http) {
    $scope.element = function(num){
        var element_id = num;  
        $http.get("customers.php",{params:{"id":element_id}}).then(function (response) {
            $scope.myData = response.data;
        });
    };

    });

PHP code

  http_response_code(404);
  header("Content-Type: application/json");
  exit();

When element_id = 6 I want to fire the http_response_code. Only thing is I'm not seeing anything. The php was taken from the schools example. What am I doing wrong? How do I get the 404 message to display? Am I even thinking of this in the right context? I've some article online but nothing really helps me much. Thanks in advance.

What am I doing wrong?

You are not handling non-success status codes in your $http request.

If you want to get the 404 message then you need to handle the error in your error callback like so:

app.controller('customersCtrl',
  function($scope, $http) {

    $scope.element = function(num) {

      var element_id = num;

      $http.get("customers.php", {
        params: {
          "id": element_id
        }
      }).then(function(response) {

        $scope.myData = response.data;

      }, function(error) {

        // this is where you handle your error
        console.log(error);

      });
    };

  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM