0

here's my JS code:

var person = [];
    person[0] = "John";
    person[1] = "Doe";
    person[2] = 46;
    var myData = JSON.stringify(person);
    $.ajax({
        type: "POST",   
        url: "test.php",
        dataType : "text", 
        contentType: "application/json; charset=utf-8",
        data: myData,
        success: function(answer) {
            alert(answer);
        },
        complete: function() {
        },
        error: function(jqXHR, errorText, errorThrown) {
            alert(jqXHR+" - "+errorText+" - "+errorThrown);
        }
    });

And here's php:

if(isset($_POST['myData']))
{
 echo "ok";
}
else
{
 echo "not_ok";
}
?>

And it always returns "not_ok". Why my PHP code can't retrieve JSON? What I'am doing wrong? Could anyone explain it to me?

2
  • You should use json_encode(); ! Commented Nov 17, 2016 at 13:13
  • 1
    data: {myData : myData}, Commented Nov 17, 2016 at 13:14

3 Answers 3

1

Try below

dataType : "json" : tells jQuery that you want it to parse the returned JSON

json_encode() : PHP function encodes array in json format.

JavaScript

var person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;

$.ajax({
    type: "POST",   
    url: "test.php",
    dataType : "json",  // Set datatype json
    data: {myData : person}, // Request Parameters
    success: function(answer) {
        console.log(answer); // JSON Response
    },
    complete: function() {
    },
    error: function(jqXHR, errorText, errorThrown) {
        alert(jqXHR+" - "+errorText+" - "+errorThrown);
    }
});

PHP

<?php
    if(isset($_POST['myData'])){
        $status = "ok";
    }
    else
    {
        $status = "Not Ok";
    }
    echo json_encode(array("status" => $status));
    exit;
?>

Output

Object {status: "ok"}
Sign up to request clarification or add additional context in comments.

7 Comments

It's me again. I was wrong.... it returns "Not ok". Dunno why, i copied and pasted your code instead of mine.
Pls write print_r($_POST); before if(isset($_POST['myData'])){ and show us output
Array ( ) {"status":"Not Ok"}
Please replace data: {'myData' : person}, with data: {myData : person}, and let us know output
Weird. After that change it works: "Object { status: "ok" }". Until i added "print_r($_POST);" in php, cuz after that alert occurs: "[object Object] - parsererror - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data"
|
0

You need some keys to get your data from the post/get variables

 data:{firstname:"John",lastname:"Doe",age:45},

php:

$_POST['firstname']

or

data:{data:["John","Doe",45]},

if you really want a array

 $_POST['data']

Comments

0

You are converting your array to string in json format without any name. But you can post array in ajax post, you dont need to stringify it. you can pass your array as it is

data: {myData: person},



var person = [];
    person[0] = "John";
    person[1] = "Doe";
    person[2] = 46;

$.ajax({
    type: "POST",   
    url: "test.php",
    dataType : "text", 
    contentType: "application/json; charset=utf-8",
    data: {myData: person},
    success: function(answer) {
        alert(answer);
    },
    complete: function() {
    },
    error: function(jqXHR, errorText, errorThrown) {
        alert(jqXHR+" - "+errorText+" - "+errorThrown);
    }
});

Comments

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.