PHP 和 Angular $http ajax 的问题
Trouble with PHP and Angular $http ajax
所以用户使用日期选择器输入,其值是通过ajax发送到PHP的值,PHP将返回包含以下内容$anoArray
的对象数组。
第一个问题:ajax 只会使用 jquery 成功;使用 Angular $http 返回如下所示"DATA WASNT AN INT"
(即使在设置标头时也是如此,因为许多人之前已经发布过)...最终我想改用本机 angular $http 服务。我在下面的内容确实返回数据,但给我留下了第二个问题。
第二个问题:我希望做各种事情,例如对对象进行排序并在返回数据后将某些键:值值设置为变量,但我似乎无法以能够做到这一点的格式返回数据。注意*我对编程相当陌生(六个月自学),任何帮助都值得赞赏。
应该返回的内容:
[
{"nameF":"harry","nameL":"chicken","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"},
{"nameF":"johnny","nameL":"appleseed","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"}
]
我的角度控制器:
app.controller('scheduleController', ['$scope', '$window', function($scope, $window) {
$scope.shifts = [];
$scope.shiftAjax = function(){
var x = $('#scheduleDate').val();
$.ajax({
method: 'post',
url: 'getSchedule.php',
data: {scheduleDate: x},
})
.done(function(response){
$scope.shifts = response;
console.log(response);
});
}
}]);
控制台日志显示我期望的数据,但我不能将其用作迭代的 javascript 对象,如果将 .done 更改为:$scope.shifts = $scope.shifts = JSON.parse(response);
,那么我只是得到了一个[object Object]
的控制台,我仍然无法迭代或从中获取键:值。
PHP post url (getSchedule.php):
<?php
$startTime = strtotime($_POST['scheduleDate']);
include_once('../../classes/scheduler.php');
if(is_int($startTime)){
$var = new scheduler; //SENT TO classes/scheduler.php
$jsonArray = $var->getSchedule($startTime);
}else{
echo "DATA WASNT AN INT";
};?>
小时在线引导我找到了这个解决方案......最后:
控制器:
app.controller('scheduleController', ['$scope', '$window', '$http', function($scope, $window, $http) {
$scope.shifts = [];
$scope.shiftAjax = function(){
$http({
url: 'getSchedule.php',
method: 'POST',
data: $.param({'scheduleDate': $scope.input}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.then( function(response) {
$scope.result = response.data;
}, function () {
console.log('ERROR: ANGULAR AJAX FAILED');
});
}
}]);
这部分的解决方案是正确设置 $.param 以及标头。
PHP Post url (getSchedule.php):
<?php
$data = $_POST['scheduleDate'];
$startTime = strtotime($data);
include_once('scheduler.php');
if(is_int($startTime)){
$var = new scheduler;
$jsonArray = $var->getSchedule($startTime);
}else{
echo " DATA WASNT AN INT";
};
?>
不知道为什么会这样,但是在调用$_POST
之前将放在一个单独的变量中strtotime()
是解决问题的踢球者。
现在返回的是:
[
0:{"nameF":"harry","nameL":"chicken","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"},
1:{"nameF":"johnny","nameL":"appleseed","start":"1456790450","end":"1456790490","location":"Station 1","comments":"these are comments"}
]
现在,我可以访问在.then
中使用点表示法返回的对象例如:
$scope.result[0].start //which returns 1456790450
- IE出现奇怪的ajax问题
- 发送表单数组值会遇到ajax问题
- 跨域 Javascript/AJAX 问题
- 尝试加载 JSON 时出现跨域 AJAX 问题
- AJAX 问题:按钮不再响应
- 节点JS和ajax问题
- Ajax问题(可能是由于encodeURIComponent字符)
- PHP文件下载与ajax问题
- Ajax-问题-将JavaScriptVar发送到PHP脚本
- AJAX问题"加载更多"按钮:点击后消失
- Ajax问题与输入,文本区域模糊
- 面对JSP的AJAX问题
- 跨浏览器Ajax问题
- Jquery移动Ajax问题
- jQuery AJAX问题?或者JS OOP作用域问题
- internet explorer上的jquery ajax问题
- 编码器(CSRF) jQuery ajax问题
- 数据表构建表从Ajax问题,ok从文件,参数丢失?表中无数据
- Jquery ajax问题-即使状态码是200也会出错
- URL参数和AJAX问题