0

I'm trying to send a JSON object to PHP through ajax. But am getting null value on PHP side.

Jquery code

 t_data = [{"name":"John", "age":30, "car":null},{"name":"John", "age":30, "car":null}]      
      $.ajax({
        url: "someupdate",
        data: {"test":JSON.stringify(t_data)},
        dataType: "json",
        type: "POST",
        beforeSend: function () {
          startLoading('Some  Update in progress, please wait');
        },
        complete: function () {
          stopLoading();
        },
        success: function (data, textStatus, jqXHR) {
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
      });
    }

PHP Side

$request = $this->getRequest();
        if ($request->isPost()) {
            $configDetails = $this->params()->fromPost('test');
   var_dump($configDetails);
var_dump(json_decode($configDetails);

Output

string(153) "[{"name":"John","age":30,"car":null},{"name":"John","age":30,"car":null}]" NULL

As you can see in the output the response is visible in the required format when am not using json decode. I wonder why it showing null value when I decoded.

Could anyone kindly help me to identify what is missing my implementation for getting the result as expected

#UPDATE

  1. I tried without JSON.stringify and got below as the output
array(4) { [0]=> bool(false) [1]=> bool(false) [2]=> bool(false) [3]=> bool(false) }
  1. Someone could kindly suggest a betterway for passing the JSON object from Jquery to PHP (Zend Framework is what am using)
  2. I'm actually creating an object using
function createJSON() {
    jsonObj = [];
    $("input[class=email]").each(function() {

        var id = $(this).attr("title");
        var email = $(this).val();

        item = {}
        item ["title"] = id;
        item ["email"] = email;

        jsonObj.push(item);
    });

    console.log(jsonObj);
}

1 Answer 1

0

I think the string you're passing is somehow incorrect (maybe is UTF-16 or some obscure encoding)

string(73) "[{"name":"John","age":30,"car":null},{"name":"John","age":30,"car":null}]"

https://3v4l.org/l7dOn

Notice how the above says string(73) and your says string(153)

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

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.