Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to throw an exception from php to angular $http service
    primarykey
    data
    text
    <p>i am working on a blog in angularJS and i use php to generate a json with data from a database. My angular 'get article' function looks like this</p> <pre><code>$scope.getDetail = function() { $http.get('php/blogGetArticle.php?id=2').success(function(json) { $scope.jsonDetail = json; alert('ok'); }).error(function() { alert('error'); }); }; </code></pre> <p>and my php 'blogGetArticle.php' looks like this.</p> <pre><code>&lt;?php $id = $_GET['id']; $dbhost = "localhost"; $dbport = "5432"; $dbname = "pd"; $dbuser = "postgres"; $dbpass = "123"; $connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass); if(!$connect) die("error 0"); // connect error $query = "SELECT * FROM blog WHERE id=" . $id; $result = pg_query($connect, $query); if(!$result) die('error 1'); // query error $row = pg_fetch_row($result); $json = '{'; $json .= '"id":"' . addslashes($row[0]) . '",'; $json .= '"title":"' . addslashes($row[1]) . '",'; $json .= '"message":"' . addslashes($row[2]) . '",'; $json .= '"category":"' . addslashes($row[4]) . '"'; $json .= '}'; echo $json; ?&gt; </code></pre> <p>Now what i try to do is i try to make the angular function take to the .error branch when i call something inside php .. i dont know how to explain well. Example when i have in php an id that is lower than 10, i want the angular function to throw an exception but i want to make it from php file to throw that exception to angular function.</p> <p>Thank you, Daniel!</p> <p>EDIT: so how do i throw a 4xx or 5xx error ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload