Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Php ajax不会返回有用的数据_Php_Jquery_Json - Fatal编程技术网

Php ajax不会返回有用的数据

Php ajax不会返回有用的数据,php,jquery,json,Php,Jquery,Json,我的文件中有一个jquery脚本,上面写着: <script type="text/javascript"> $(document).ready(function(e){ $('#myButton').click(function(e){ var formdata = {name: 'Alan', hobby: 'boxing'}; var submiturl = 'http://localhost/cake/gronsters/testJSO

我的文件中有一个jquery脚本,上面写着:

<script type="text/javascript">
$(document).ready(function(e){
    $('#myButton').click(function(e){
        var formdata = {name: 'Alan', hobby: 'boxing'};
        var submiturl = 'http://localhost/cake/gronsters/testJSON/';
        $.ajax({
            type: "POST", 
            url: submiturl,
            data: formdata,
            success: function(message){
                console.log(message);
            }
        });
        e.preventDefault();
    })
});
public function testJSON(){
    $name = $this->request->data['name'] . '-Sue';
    $hobby = $this->request->data['hobby'] . ' for donuts';
    $data = array(  'name' => $name, 'hobby' => $hobby);
    echo json_encode($data);
}
console.log查找消息的地方给了我{name:Alan Sue,hobby:boxing for donuts},这似乎是正确的,只是后面紧接着是我网页的完整html。如果我尝试console.logmessage.name,它会显示“未定义”


这里发生了什么?

听起来您的/testJSON/页面输出的不仅仅是在公共函数中写入的内容。如果您使用的是mvc或任何类型的框架,您必须在echo json_编码后立即退出或退出,否则页面的其他部分可能仍会呈现。

听起来您的/testJSON/页面输出的不仅仅是公共函数中写入的内容。如果您使用的是mvc或任何类型的框架,则必须在echo json_编码后立即退出或退出,否则页面的其他部分可能仍会呈现。

在尝试记录message.name之前,将消息变量解析为json

data = $.parseJSON(message);
console.log(data.name);

另外,在echo语句之后退出php脚本,并选中..

在尝试记录message.name之前将消息变量解析为json

data = $.parseJSON(message);
console.log(data.name);

在echo语句之后,在php脚本中退出并检查..

您的路径是正确的,尽管正如其他人所提到的,您的网站似乎使用了MVC框架,并且在显示JSON字符串后包含了网页的内容。如果响应中有其他随机文本或字符,jQuery/JavaScript无法解析JSON。您需要使用exit;要么死;在JSON被回送后,如下例所示

public function testJSON(){
    $name = $this->request->data['name'] . '-Sue';
    $hobby = $this->request->data['hobby'] . ' for donuts';
    $data = array(  'name' => $name, 'hobby' => $hobby);
    // Will halt the script and echo the json-encoded data.
    die(json_encode($data));
}
此外,您还需要确保jQuery将响应解析为JSON。默认情况下,它将尝试对返回的数据类型进行智能猜测,但您不能总是依赖于此。您应该向AJAX调用添加dataType选项,如以下示例所示

<script type="text/javascript">
$(document).ready(function(e){
    $('#myButton').click(function(e){
        // It is best practice to "preventDefault" before any code is executed.
        // It will not cause the ajax call to halt.
        e.preventDefault();
        var formdata = {name: 'Alan', hobby: 'boxing'};
        var submiturl = 'http://localhost/cake/gronsters/testJSON/';
        $.ajax({
            type: "POST", 
            url: submiturl,
            data: formdata,
            // This is the proper data type for JSON strings.
            dataType: 'json',
            success: function(message){
                // JSON is parsed into an object, so you cannot just output
                // "message" as it will just show "[object Object]".
                console.log(message.name);
            }
        });
    });
});
</script>
有关$.ajax的更多选项和用法信息
有关$.post的更多信息

您走的是正确的道路,尽管正如其他人所提到的,您的网站似乎使用了MVC框架,它在显示JSON字符串后包含了网页的内容。如果响应中有其他随机文本或字符,jQuery/JavaScript无法解析JSON。您需要使用exit;要么死;在JSON被回送后,如下例所示

public function testJSON(){
    $name = $this->request->data['name'] . '-Sue';
    $hobby = $this->request->data['hobby'] . ' for donuts';
    $data = array(  'name' => $name, 'hobby' => $hobby);
    // Will halt the script and echo the json-encoded data.
    die(json_encode($data));
}
此外,您还需要确保jQuery将响应解析为JSON。默认情况下,它将尝试对返回的数据类型进行智能猜测,但您不能总是依赖于此。您应该向AJAX调用添加dataType选项,如以下示例所示

<script type="text/javascript">
$(document).ready(function(e){
    $('#myButton').click(function(e){
        // It is best practice to "preventDefault" before any code is executed.
        // It will not cause the ajax call to halt.
        e.preventDefault();
        var formdata = {name: 'Alan', hobby: 'boxing'};
        var submiturl = 'http://localhost/cake/gronsters/testJSON/';
        $.ajax({
            type: "POST", 
            url: submiturl,
            data: formdata,
            // This is the proper data type for JSON strings.
            dataType: 'json',
            success: function(message){
                // JSON is parsed into an object, so you cannot just output
                // "message" as it will just show "[object Object]".
                console.log(message.name);
            }
        });
    });
});
</script>
有关$.ajax的更多选项和用法信息
有关$.post的更多信息

是testJSON中的所有代码吗?在设置ajax调用时,您可能需要设置数据类型:“json”。我知道jQuery对该类型有一个聪明的猜测,但谁知道呢,也许它做错了。是的,仅此而已——它只是我的cake php应用程序控制器中的一个函数,是不是testJSON中的所有代码?在设置ajax调用时,您可能需要设置数据类型:“json”。我知道jQuery对类型有一个聪明的猜测,但谁知道呢,也许它做错了。是的,仅此而已——它只是我的cake php应用程序控制器中的一个函数,我现在无法测试,但听起来不错。我正在使用cake php。我现在无法测试,但听起来不错。我正在使用cake php。谢谢-我回到电脑前会试试。谢谢-我回到电脑前会试试。