Can't get the parameter from front end using AngularJS

56 Views Asked by At

I tried to separate front-end and back-end by using AngularJS and PHP, but I can't get the parameter passed from front-end. The strange thing is, I am able to put the parameter into database then echo it. But before that, I can't echo the parameter. I tried to use isset to judge whether the parameter is passed to back-end but it didn't work.

Request Header:

Request Header

Parameters returned from back-end:

Parameters returned from back-end

I want the change the parameter "page" to 2, which is the number passed from front-end, following is part of the validation code of back-end:

<?php
require_once 'config.php';
//current page
$page = 1;
$post = json_decode(file_get_contents("php://input"));
if (isset($post)){
   $page = $post -> page;
}else{
   $page =3;
}

The data returned is 3, which is not the posted number.

Ajax from front-end (AngularJS):

$scope.selectPage = function(page){
        if(page<1 || page>$scope.pages){
        }else{
            $http.post('getMessage.php',{
                'page':2
            }).then(
                function success(){
                    $scope.getAllMessage();
                }, function error() {
                    console.log("select failed");
                });
        }

    };

Json data created by back-end:

while ($messages = mysqli_fetch_assoc($result)) {
    if ($outpm != "") {
        $outpm .= ",";
    }
    $outpm .= '{"mid":"' . $messages["mid"] . '",';
    $outpm .= '"uid":"' . $messages["uid"] . '",';
    $outpm .= '"username":"' . $messages["username"] . '",';
    $outpm .= '"content":"' . $messages["content"] . '",';
    $outpm .= '"date":"' . $messages["date"] . '",';
    $outpm .= '"parent_id":"' . $messages["parent_id"] . '"}';
};
$outpp .= '{"page":"' . $page . '",';
$outpp .= '"offset":"' . $offset . '",';
$outpp .= '"total_msg":"' . $total_msg . '",';
$outpp .= '"total_page":"' . $total_page . '",';
$outpp .= '"show_page":"' . $show_page . '",';
$outpp .= '"page_offset":"' . $page_offset . '",';
$outpp .= '"start":"' . $start . '",';
$outpp .= '"end":"' . $end . '"}';
//number of
$outp = '{"messages":[' . $outpm . '],"pagination":' . $outpp . '}';
0

There are 0 best solutions below