我是angular js的新手。我有一个test.html页面。
<标题> test.html h1> 我运行test.html时,请求(调用)将转到customer .php页面并返回响应。请建议我如何将请求发送到函数?我在firebug中得到以下错误。
得到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"
标题>在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,单引号周围的键和值是无效的,我已经把它改成双引号"这是有效的。
正如@charlietfl所说的,json_encode JSON响应而不是自己编写JSON响应是更好的实践。
您可以在post方法中发送您的函数名,并在php页面上获取该方法,使用该方法您可以执行函数。
app.controller('customersCtrl', function($scope, $http) {
var request = $http.post('acctUpdate.php', {fun: "testdata"});
request.success(
function( html ) {
console.log(html);
}
); });