weixin_33711641 2020-03-24 23:02 采纳率: 0%
浏览 34

ajax调用返回NULL

I´m playing around with an AJAX call. I try to pass data to a sql query and receive back the data in my JS file. Somehow always the error function get called in the JavaScript

while the XHR header returns this in the preview: Array ( [address] => Street 12 [name] => twelve [id] => 12 [surname] => twelve )

File data_3.php

<?php
require 'assets/external/db.php';
$data =  file_get_contents('php://input');
$data = json_decode($data, true);

$id = $data['id'];

$queryData = $mysqli->prepare("
SELECT
    id
FROM
    listing
 WHERE id = ?
");
$queryData->bind_param('s', $id);
$queryData->execute();
$data = $queryData->get_result();

$data = mysqli_fetch_all($data, MYSQLI_ASSOC);
mysqli_close($mysqli);
echo json_encode($data);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/json_test.js"></script>

<script>
    var ajaxData = {
        "address": "Street 12",
        "name": "twelve",
        "id": 12,
        "surname": "twelve"
    };
    loadData('assets/external/data_3.php', ajaxData);
</script>

File json_test.js

function loadData(url, ajaxData) {
console.log('JS loaded');
$.ajax({
    url: url,
    type: "POST",
    data: JSON.stringify(ajaxData),
    async: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (results) {
        console.log(results);
    },
    error: function (e) {
        console.log(e);
    }
});
}

How can I call the sql query correctly, so that $id get filled and the result gets back to the JS function loadData?

  • 写回答

1条回答 默认 最新

  • weixin_33736048 2020-03-25 00:26
    关注

    Problem solved by split the sql part to a new file

    评论

报告相同问题?