简体   繁体   English

带有响应array []的jQuery Ajax PHP POST方法instad发送了数据。 如何获得发送值作为响应?

[英]jQuery Ajax PHP POST method with response array[] instad sent data. How to get send values in response?

I am creating simple application which will insert log data into database table. 我正在创建一个简单的应用程序,它将日志数据插入数据库表。

Going with jQuery Ajax POST method and JSON format for data. 使用jQuery Ajax POST方法和JSON格式的数据。

My index.php has HTML and this code: 我的index.php具有HTML和以下代码:

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery.ajax({
        type: "POST",
        url: "http://www.example.com/ajax_model.php",
        data: {
            "log_session": "27738d7552b75ae395ae1138adf4fa60",
            "log_ip": "1.2.3.4",
            "log_vrijeme": "2018-11-23 01:22:47",
            "log_model": "12345"
        },
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        success: function(response) {
            console.log(response);
            alert(response);
        },
        error: function(error) {
            console.log(error);
        }
    });
});
</script>

My ajax_model.php - which gives me response Array[] instad the data which is sent: 我的ajax_model.php-给我响应Array[]设置发送的数据:

<?php
if(isset($_POST)){
    header('Content-Type: application/json');
    echo json_encode($_POST);
    exit;
}
?>

I am getting result in my console.log() and alert() - Array[] . 我在console.log()alert() - Array[]得到结果。 How can I output my sent response to check if the data was correctly sent over Ajax? 如何输出已发送的响应以检查数据是否通过Ajax正确发送?

Am I missing some brackets like [] or {} ? 我是否缺少[]{}类的括号? Or do I need to add something to my ajax_model.php file? 还是我需要在ajax_model.php文件中添加一些内容?

Thank you for suggestion and provided information. 感谢您的建议和提供的信息。

You aren't sending JSON so get rid of: 您没有发送JSON,因此请摆脱:

 contentType: 'application/json; charset=utf-8',

That will cause $_POST to be empty as the default contentType is: 这将导致$ _POST为空,因为默认的contentType为:

application/x-www-form-urlencoded; charset=UTF-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM