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

harish

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"

CT14.IT

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

using timer to get runtime of a funtion

From Dev

Initialize AngularJS Constant using $http.get call response

From Dev

AngularJS : to get response when hitting mail.php using $http

From Dev

How to assign a $http.get response to a variable AngularJS

From Dev

Getting ECONNRESET response using node's http.get function

From Dev

Get $http response error status on AngularJS

From Dev

how get response http header?

From Dev

how get response http header?

From Dev

how to get response of http request

From Dev

How to access JSON data in $http.get method's response?

From Dev

How to get the response content of an HTTP 404 response

From Dev

How to get port number from an HTTP response, using Python?

From Dev

How to return AngularJS Function's $http response?

From Dev

Not able to get image as json response using angularJS?

From Dev

How to call $http.get() in Service using AngularJS

From Dev

Processing HTTP Get response using ajax in html

From Dev

How to get Response after POST in AngularJS?

From Dev

how to get headers from response in angularjs

From Dev

AngularJS - get headers in response

From Dev

Restangular: How to get HTTP response header?

From Dev

How to get http response header in VBScript

From Dev

how to get the server name from a http response

From Dev

How to get http response header in VBScript

From Dev

Restangular: How to get HTTP response header?

From Dev

How to cache and share Http get() response?

From Dev

How to get an Http response code in a soap client?

From Dev

How to get full HTTP request (not response) headers

From Dev

How to get exact response from http services

From Dev

AngularJS $http get data object displays status code instead of response

Related Related

HotTag

Archive