0

I'm having a problem like many other people, being able to read a PHP array variable after having sent it through ajax() post. Ajax is succeeding and displaying the returned data, which is NULL. I have already thoroughly researched SO solutions for this JSON/PHP problem, and my problem description shows 'almost' EVERY solution on SO so far.

On the PHP side I've tried:

$data = json_decode(file_get_contents('php://input'), true);
var_dump($data);

(Skipping over $HTTP_RAW_POST_DATA (deprecated) because it's equal to file_get_contents('php://input')

and also:

$data = json_decode($_POST["a_arr"], true);

I've already tried clearing the UTF-8 BOM with the sed command

sed '1s/^\xEF\xBB\xBF//' < index.html > index2.html

My .ajax() looks like this:

$.ajax ({
    url:"file.php",
    method:"post",
    contentType: "application/json; charset=utf-8",
    data: { a_arr : JSON.stringify(arr) },
    })
    .done(function(response){
        $("#status").html(response);
    });

On the Javascript side here is my array:

var arr = [{"name":_name, "phone":_phone, "email":_email, "repname":repname, "repnumber":repnumber, "office":office}];
ajax_post(arr);

I've checked to make sure there is no JSON formatting error, the following successfully shows me a valid JSON formatted array:

var data_arr = JSON.stringify(arr);
document.getElementById("status").innerHTML = data_arr;
2
  • Possible duplicate of How can I use JQuery to post JSON data? Commented May 17, 2019 at 20:28
  • I have thoroughly reviewed SO for similar JSON/PHP cases, and though while close in appearance, I have found no solution as my problem description illustrates. Commented May 20, 2019 at 15:35

1 Answer 1

1

You need to change to:

data: JSON.stringify(arr),

When you give an object to the data: option, it converts it to URL-encoded format, not JSON.

Or you can leave the data: option as it is, but get rid of the contentType: option, and then you should use

$data = json_decode($_POST['a_arr'], true);
Sign up to request clarification or add additional context in comments.

6 Comments

Very grateful for the input and information! Ok, I tried your suggesttion, relying on data: to push URL encoding, I removed the name and put: data: JSON.stringify(arr), This also returns NULL! Then I put the data: element back to JSON format: data: { a_arr : JSON.stringify(arr) }, Commented out the contentType: //contentType: "application/json; charset=utf-8", And lastly switched from php://input over to: $data = json_decode($_POST['a_arr'], true); $var_dump($data); is still NULL in both cases :(
What version of jQuery are you using? The method: option was added in 1.9.0, before that it was type:.
What do you see in var_dump(file_get_contents("php://input"));?
Using: jquery-1.8.3.min.js
Wow, nice suggestion: the return data is: string(0) "" Which is funny because I get a valid javascript JSON string pre .ajax()
|

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.