-5

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"

harish
  • 13
  • 6
  • Don't create json manually...it is error prone. The string you created will not be output as valid json – charlietfl Jul 30 '15 at 12:50
  • 1
    If you are new to angular, then why don't you learn it through their [tutorial](https://docs.angularjs.org/tutorial/step_01) which will help you understand how requests are made and how responses are handled. – callmekatootie Jul 30 '15 at 12:51
  • 1
    Also provide a more specific problem statement in your questions – charlietfl Jul 30 '15 at 12:53

2 Answers2

1

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.

CT14.IT
  • 1,635
  • 1
  • 15
  • 31
  • but i want to pass the function from my angular controller – harish Jul 30 '15 at 12:46
  • What function in your angular controller? – CT14.IT Jul 30 '15 at 12:47
  • i want the response of testdata(). i will pass this function from my angular controller – harish Jul 30 '15 at 12:48
  • The code I provided returns the response of testdata(). If that's not what you're after, I don't follow. – CT14.IT Jul 30 '15 at 12:49
  • won't be valid json in response – charlietfl Jul 30 '15 at 12:50
  • Yes, right. but in cust.php there is more than one function so i wants the response of testdata() only. – harish Jul 30 '15 at 12:52
  • @charlietfl true, I've updated my answer with correct JSON (and you're right, it should be encoded, not hand written). harish, you need to be more clear in your question then. – CT14.IT Jul 30 '15 at 12:55
  • @CT14.IT doesn't help just changing quotes in your anwser without explanation also or that change from OP code can easily go undetected – charlietfl Jul 30 '15 at 12:56
  • @charlietfl:- i agree with you. can i pass testdata() function name from $http.get("../../../frontend/controllers/cust.php")??? so that i will directly get the response of testdata() function only – harish Jul 30 '15 at 12:58
  • @harish if it is a case of which function then use some sort of GET param like `action` and decide in php controller what to send based on it's value – charlietfl Jul 30 '15 at 13:00
  • Now, i get this error: GET http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/testdata 404 Not Found angular.min.js (line 86) "NetworkError: 404 Not Found - http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/testdata" – harish Jul 31 '15 at 05:37
  • I update my code here. Please check it and let me know. – harish Jul 31 '15 at 05:39
  • exception 'yii\base\InvalidConfigException' with message 'Unknown component ID: db' in /var/www/yii2-angular-seed-master/vendor/yiisoft/yii2/di/ServiceLocator.php:136 Stack trace: #0 /var/www/yii2-angular-seed-master/vendor/yiisoft/yii2/base/Application.php(491): yii\di\ServiceLocator->get('db') #1 /var/www/yii2-angular-seed-master/vendor/yiisoft/yii2/db/ActiveRecord.php(135): yii\base\Application->getDb() #2 /var/www/yii2-angular-seed-master/vendor/yiisoft/yii2/db/ActiveQuery.php(305): yii\db\ActiveRecord::getDb() – harish Jul 31 '15 at 10:30
1

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);
        }
        ); });
Amit Rana
  • 190
  • 5
  • Now there is no error. But there is nothing in firebug it shows (an empty string) – harish Jul 31 '15 at 05:57
  • sent response back from the response page. – Amit Rana Jul 31 '15 at 05:59
  • how? look here is my funtion:- function actionIndex(){ $id = 2015; return $id; } – harish Jul 31 '15 at 06:02
  • is it returning 2015 ? – Amit Rana Jul 31 '15 at 06:08
  • in yii we are getting json by //read the post input (use this technique if you have no post variable name): $post = file_get_contents("php://input"); //decode json post input as php array: $data = CJSON::decode($post, true); – Amit Rana Jul 31 '15 at 06:09
  • RESPONSE :-
    Fatal error: Class 'yii\web\Controller' not found in /var/www/yii2-angular-seed-master/frontend/controllers/CategoriesController.php on line 20
    – harish Jul 31 '15 at 06:18
  • Look my categories controler – harish Jul 31 '15 at 06:20
  • do you know actual work flow of yii ? as your code i think you should first go for tutorials, you are hitting the wrong url. The URL must be : http://localhost/advanced/frontend/web/index.php?r=category You will get your desired result I am an expert in YII as well – Amit Rana Jul 31 '15 at 07:38
  • first direct hit the yii url through browser and when you get 1000 on screen copy that url and paste it to angular url. – Amit Rana Jul 31 '15 at 08:10
  • Fatal error: Class 'Controller' not found in /var/www/yii2-angular-seed-master/frontend/controllers/CategoryController.php on line 8
    – harish Jul 31 '15 at 09:09