Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我不能通过ajax将回调数组从PHP传递到jquery?_Javascript_Php_Jquery_Json_Ajax - Fatal编程技术网

Javascript 为什么我不能通过ajax将回调数组从PHP传递到jquery?

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文件:

<?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
    }
?>