出于某种原因,我正在进行的这个ajax调用是在控制台日志中返回错误状态......我似乎无法弄清楚为什么,一切看起来都对我不对!
AJAX:
$(document).ready( function() {
$('.load-more').click(function(e) {
e.preventDefault();
console.log('Starting AJAX');
var $this = $(this);
var load = $this.data('current-page') + 1;
var parent = $this.data('parent');
var properties = {
snippet: 'infiniteScroll',
limit: 3,
offset: 0,
parents: 22,
depth: 999,
sortby: 'publishedon',
showHidden: 1,
debug: 1,
tpl: 'infiniteHomePageTpl',
};
$this.data('current-page', load);
$.ajax({
type: "POST",
url: "/ajax.processor",
data: properties,
cache: false,
dataType: "json",
success: function(response) {
if(response.status == 'success'){
console.log('Success');
}else if(response.status == 'error'){
console.log('data error');
}else{
console.log('some other error');
}
},
error: function(response){
console.log('an error has occurred' + response);
},
}); // ajax
});
});// document ready
正在调用处理器:
<?php
// $modx->log(modX::LOG_LEVEL_ERROR,'Running ajax.processor, snippet = '.$_POST['snippet'].' formdata = '. print_r($_POST, TRUE));
$output = $modx->runSnippet($_POST['snippet'],array(
'snippet' => $_POST['snippet'],
'data' => $_POST
));
// set the header for the ajax call
$output = json_encode($output);
header('Content-type: application/json');
$modx->log(modX::LOG_LEVEL_ERROR,'Should be json = '.$output);
echo $output;
处理器执行的代码:
<?php
//$modx->log(modX::LOG_LEVEL_ERROR, 'Running infinitescroll snippet' . print_r($scriptProperties, TRUE));
$output = array(
'status' => 'success',
'msg' => 'a message of some type',
'whatever' => 'this is some data',
);
return $output;
处理器中的日志记录行将json输出到日志:
Should be json = {"status":"success","msg":"a message of some type","whatever":"this is some data"}
所以我的javascript~应该〜让json回来了,但它没有达到成功条件,我在控制台日志中获得的是:
an error has occurred[object Object]
我在这里做错了什么?
答案 0 :(得分:0)
你需要对php数组进行json_encode,就像Kaleb已经评论过的那样。 http://php.net/manual/en/function.json-encode.php
所以你的代码应该是:
//$modx->log(modX::LOG_LEVEL_ERROR, 'Running infinitescroll snippet' . print_r($scriptProperties, TRUE));
$output = array(
'status' => 'success',
'msg' => 'a message of some type',
'whatever' => 'this is some data',
);
$json = json_encode($output);
return $json;
我还建议您使用Firebug来监控您的javascript,而不是记录控制台消息:http://getfirebug.com/
答案 1 :(得分:0)
尝试使用HTTP客户端(例如Postman(或甚至cURL))来模拟您的AJAX调用。这样可以轻松检查响应的标题和内容。
此外,当调用错误处理程序时,第一个参数将始终是XHR对象。如果你执行console.log(response);
(没有字符串转换),你应该能够检查其内容。对你来说可能更有用的是另外两个参数:
错误 类型:函数(jqXHR jqXHR,String textStatus,String errorThrown)
更多信息位于the jQuery.ajax docs。
答案 2 :(得分:0)
您实际上需要一个json内容类型标题和来对输出进行编码:
<?php
header('Content-Type: application/json');
//$modx->log(modX::LOG_LEVEL_ERROR, 'Running infinitescroll snippet' . print_r($scriptProperties, TRUE));
$output = array(
'status' => 'success',
'msg' => 'a message of some type',
'whatever' => 'this is some data',
);
echo json_encode($output);
如果你想知道为什么会发生这种情况,你可以使用errorThrown
来$.ajax.fail()
:
$.ajax({
type: "POST",
url: "/ajax.php",
data: properties,
cache: false,
dataType: "json",
success: function(response) {
//your code
},
error: function(response){
//your code
},
}).fail(function(jqXHR,textStatus,errorThrown) {
console.log(errorThrown);
});