这是php中的响应。我可以确认数据是好的。
$ajax_response = array(
'product_code' => $ajax_products,
'filter' => $ajax_filter
);
echo json_encode($ajax_response);
exit();
以下是javascript中的代码:
$('#pr_category_filter').submit(function (event) {
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: 'json',
cache: false,
success: function (data) {
if (data.product_code != null) {
$('#pagination_contents').replaceWith(data.product_code);
}
if (data.filter != null) {
$('#category_filter').replaceWith(data.filter);
}
},
error: function (request, status, error) {
return false;
}
});
event.preventDefault(event);
});
此代码在Chrome和Opera上运行良好。然而,这段代码在Firefox上不起作用,因为php"echo"显示在Firefox上,而不是ajax响应。我还尝试在javascript中放入console.debug('invoked')。Firefox中不会显示与Chrome相反的结果。你知道原因吗?
对浏览器工具开发的响应是一样的。
感谢
函数.prventDefault()不接受任何参数。
因此,Firefox可能不接受这一点,只是简单地提交了表格。然而,Chrome并没有真正关心并接受它。
所以改变
event.preventDefault(event);
进入
event.preventDefault();
这应该可以完成
您需要在PHP中使用header()
函数来指定返回AJAX调用的响应的内容类型。尝试以下操作(将其放在回应之前):
header('Content-Type', 'application/json');