[英]Angularjs :- how to get funtion's response using $http.get()?
<!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.当我运行 test.html 时,请求(调用)将转到 cust.php 页面并返回响应。 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获取http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/testdata
404 Not Found 404 未找到
"NetworkError: 404 Not Found - http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/Index " “网络错误:404 未找到 - http://localhost/yii2-angular-seed-master/frontend/controllers/CategoryController/Index ”
In cust.php you actually need to call the function as well在 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();
?>
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.编辑:我也不得不更改您的 $str,围绕键和值的单引号无效,我已将其更改为有效的双引号 "。
As @charlietfl has stated it's better practice for you to json_encode your JSON response instead of writing it yourself.正如@charlietfl 所说,最好的做法是对 JSON 响应进行 json_encode,而不是自己编写。
You can sent your function name in post method and get the method on php page, using that you can perform functions.您可以在post方法中发送您的函数名称并在php页面上获取该方法,使用它您可以执行功能。
app.controller('customersCtrl', function($scope, $http) {
var request = $http.post('acctUpdate.php', {fun: "testdata"});
request.success(
function( html ) {
console.log(html);
}
); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.