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;
  • Possible duplicate of [How can I use JQuery to post JSON data?](https://stackoverflow.com/questions/6255344/how-can-i-use-jquery-to-post-json-data) – miken32 May 17 '19 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. – user3285269 May 20 '19 at 15:35

1 Answers1

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 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 :( – user3285269 May 20 '19 at 15:40
  • What version of jQuery are you using? The `method:` option was added in 1.9.0, before that it was `type:`. – Barmar May 20 '19 at 15:52
  • What do you see in `var_dump(file_get_contents("php://input"));`? – Barmar May 20 '19 at 15:53
  • Using: jquery-1.8.3.min.js – user3285269 May 20 '19 at 16:12
  • Wow, nice suggestion: the return data is: string(0) "" Which is funny because I get a valid javascript JSON string pre .ajax() – user3285269 May 20 '19 at 16:14
  • Based on your question, I shot up to Jquery 1.12.4.min.js and I immediately got a valid string(256) response. I'm concurrently learning JSON and working with other existing Jquery projects on this web app. When I had tried upgrading to Jquery 3.3.1 it had broken functionality. Thank You! – user3285269 May 20 '19 at 16:25