简体   繁体   中英

Empty response by JQuery.ajax and object and php

I am stuck by creating my first universal code for ajax responses. I simply do not get an output here, neither in php nor in the ajax response. This must have something to be in the post data.

This is my ajax request:

        var info = {};
        //get id info
        info["ses-id"] = $("#theme").attr("scene");
        //get template info
        info["ses-template"] = $("#theme").attr("template");

        $.ajax({
            dataType: "json",
            contentType: "application/json; charset=UTF-8",
            data: JSON.stringify(info),
            type: "POST",
            url: "query.php"
        }).done(function(data, textStatus, jqXHR) {
            alert (data);
            //window.location = "?szenen";
            console.log("Data sent.");
        }).fail(function(jqXHR, textStatus, errorThrown) {
            console.log("There was an error." + errorThrown);
        });

This is my query.php so far:

    <?php 
        $return = $_POST;
        $return["json"] = json_encode($return);
        print(json_encode($return));

The output is an object where only the json entry is filled with [].

The stringified variable looks good, it's a string like this:

    {"ses-id":"1","ses-template":"2"}

Thanks for any advice!

Your problem is you are sending a json encoded string as the POST body, and then using $_POST to access it. $_POST is an array of key/value POST data. Since your data doesn't have a key, you can't access it from the $_POST array. It's just a value (a string value at that).

If you change your PHP script to be:

<?php
echo file_get_contents("php://input");
?>

It will output the JSON you passed in. Note that there is no need to do a json_encode() because the value is a string, not an array. The json you passed it was never decoded.

Alternatively, if your javascript was:

$.ajax({
    data: { "data": JSON.stringify(info) },
    type: "POST",
    url: "query.php"
})

then your post body would be:

data={"ses-id":"1","ses-template":"2"}

you could then do

<?php
echo $_POST["data"];
?>

Again noting the data you sent it was never decoded, so it's still just a string. PHP does not json_decode for you.

It works when you drop contentType and JSON.stringify:

    var info = {};
    //get id info
    info["ses-id"] = $("#theme").attr("scene");
    //get template info
    info["ses-template"] = $("#theme").attr("template");

    $.ajax({
        dataType: "json",
        data: info,
        type: "POST",
        url: "query.php"
    }).done(function(data, textStatus, jqXHR) {
       console.log(data);
        //window.location = "?szenen";
        console.log("Data sent.");
    }).fail(function(jqXHR, textStatus, errorThrown) {
        console.log("There was an error." + errorThrown);
    });

The dataType and contentType both must be deactivated, then I got responses.

    $.ajax({
        //dataType: "json",
        //contentType: "application/json; charset=UTF-8",
        data: JSON.stringify(info),
        type: "POST",
        url: "query.php"

Varying the info object results in varies of the php var but that wasn't my problem.

BIG thanks to everyone especially CJ_Wurtz who pushed me the right way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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