2

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!

10
  • 2
    Where is the succes function? Commented Mar 12, 2015 at 14:03
  • 1
    Doesn't it need to be json_decode? Commented Mar 12, 2015 at 14:03
  • 1
    @Naruto succes(), error() and complete() are deprecated in JQuery 1.8 Commented Mar 12, 2015 at 14:05
  • @Naruto From the jQuery docs: An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method Commented Mar 12, 2015 at 14:05
  • Alright, seems I've learned something new today :) Commented Mar 12, 2015 at 14:09

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I changed the line to data: { "data": JSON.stringify(info) },, but the $_POST["data"] is still empty. I don't get it.
Check if the echo file_get_contents("php://input"); works. Also updated my answer, removed the json data/content types defined. Try without those defined
I am getting data (whoooho) but it seems urlencoded. I will try to get it into the post variable.
When you remove contentType and JSON.stringify, like in my answer, then $_POST is normal array with those key value pairs as expected.
np, sorry i wasn't spot on, I assumed the jquery was correct and was just testing the POST call manually, and saw the data go through fine. Also just checked the docs, don't remove the dataType: "json" I said to, it's what decodes the json response
1

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.

Comments

0

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);
    });

1 Comment

Thanks, the result is that contentType and dataType must be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.