Javascript 为什么我不能通过ajax将回调数组从PHP传递到jquery?
我有一个大文件,但在调试问题时,我缩小了它,以消除外部的一切。基本上,我最终得到了这个.php文件:Javascript 为什么我不能通过ajax将回调数组从PHP传递到jquery?,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,我有一个大文件,但在调试问题时,我缩小了它,以消除外部的一切。基本上,我最终得到了这个.php文件: <?php if (isset($_POST['val'])) { header("Content-type: application/json; charset=utf-8"); echo json_encode(array("The first string", "first")); } ?> <script type="text
<?php
if (isset($_POST['val'])) {
header("Content-type: application/json; charset=utf-8");
echo json_encode(array("The first string", "first"));
}
?>
<script type="text/javascript" src="javascript/jquery.js"></script>
<script type="text/javascript">
$.ajax({
type: 'POST',
url: 'other',
data: {val: 'text'},
contentType: "application/json",
dataType: 'json',
success: function(result) {
console.log(result);
alert(result);
},
error: function(err) {
console.log(err);
alert('Error -> ' + err);
}
});
</script>
如果我将数据类型更改为html,并删除contentType,则会执行success函数。它通过了这个:
["The first string","first"]
<script type="text/javascript" src="javascript/jquery.js"></script>
<script type="text/javascript">
$.ajax({
type: 'POST',
[“第一个字符串”,“第一个”]
$.ajax({
键入:“POST”,
以及页面的其余代码
我需要接收json。为什么这个东西不工作?为什么我不接收json
附言。
您可能会注意到,我在url的末尾没有.php。。。
我有一个.htaccess文件,它删除了它,所以这不是问题所在。我禁用了它,并将脚本与.php一起使用,但没有任何更改。php代码已执行,但没有得到正确的回调…尝试在回显json后添加一个出口,这样就不会将下面的html返回到ajax调用中
<?
if (isset($_POST['val'])) {
header("Content-type: application/json; charset=utf-8");
echo json_encode(array("The first string", "first"));
exit; // add exit here so html below is not also returned
}
?>
该死,你真是个天才!)我花了大约两天的时间试图解决这个问题,研究并缩小范围……答案很简单。)多谢了
<?
if (isset($_POST['val'])) {
header("Content-type: application/json; charset=utf-8");
echo json_encode(array("The first string", "first"));
exit; // add exit here so html below is not also returned
}
?>