简体   繁体   中英

Angularjs :- how to get funtion's response using $http.get()?

I am new in anggular js. I have a test.html page.

test.html

    <!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14  /angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
   <!--  <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td> -->
  </tr>
</table>

</div>

<script>

var app = angular.module('myApp', []);


app.controller('customersCtrl', function($scope, $http) {
    $http.get("../../../frontend/controllers/CategoryController/Index")
.success(function (response) {
        //$scope.names = response.records;
        console.log(response);
    });
});
</script>


</body>
</html>
==================
category controller
==================
<?php
namespace frontend\controllers;

use Yii;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

 /**
 * Site controller
 */
class CategoryController extends Controller
{
    /**
      * @inheritdoc
       */
    public  $str;
    public function actionIndex(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $id = 2015;

        return $id;
       }
     }
    ?>

When i run test.html then the request(call) will go to the cust.php page and return response. Please suggest me how can i send the request to the function?

I got following error in firebug.

GET http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/testdata

404 Not Found

"NetworkError: 404 Not Found - http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/Index "

In cust.php you actually need to call the function as well

<?php
   header('Content-Type: application/json');
   function testdata(){
      $str='{"employees":[{"firstName":"John", "lastName":"Doe"},{"firstName":"Anna", "lastName":"Smith"},{"firstName":"Peter", "lastName":"Jones"}]}';
      return $str;
  }
  echo testdata();
?>

EDIT: I've had to change your $str as well, single quote surrounding keys and values are not valid, I have changed it to double quotes " which are valid.

As @charlietfl has stated it's better practice for you to json_encode your JSON response instead of writing it yourself.

You can sent your function name in post method and get the method on php page, using that you can perform functions.

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

var request = $http.post('acctUpdate.php', {fun: "testdata"});


        request.success(
        function( html ) {
        console.log(html);
        }
        ); });

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