<!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;
}
}
?>
当我运行test.html时,请求(调用)将转到cust.php页面并返回响应。 请建议我如何将请求发送到该功能?
我在萤火虫中遇到了以下错误。
获取http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/testdata
404 Not Found
&#34; NetworkError:404 Not Found - http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/Index&#34;
答案 0 :(得分:1)
在cust.php中你实际上也需要调用该函数
<?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();
?>
编辑:我也不得不改变你的$ str,关于键和值的单引号无效,我已将其改为双引号&#34;这是有效的。
正如@charlietfl已经声明为json_encode你的JSON响应而不是自己写的更好的做法。
答案 1 :(得分:1)
您可以在post方法中发送您的函数名称并在php页面上获取该方法,使用它可以执行函数。
app.controller('customersCtrl', function($scope, $http) {
var request = $http.post('acctUpdate.php', {fun: "testdata"});
request.success(
function( html ) {
console.log(html);
}
); });