1

This is my jQuery code

function ajaxCall(dataStream) {
    $.ajax({
        type: 'post',
        url: 'be/email.php',
        data: JSON.stringify(dataStream),
        contentType: "application/json; charset=utf-8",
        traditional: true,
        success: function (data) {
    }
});

The dataStream is { email: "da@gt.lo", skype: "dasd"}.

Is this correct way of sending a json to PHP. I can see the json being sent in dev tools. But i cannot get them from my php.

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
Jagadeesh J
  • 1,211
  • 3
  • 11
  • 16
  • Yes, that's correct. Your problem is almost certainly on the PHP side. – Quentin Oct 06 '13 at 09:42
  • possible duplicate of [Issue reading HTTP request body from a JSON POST in PHP](http://stackoverflow.com/questions/7047870/issue-reading-http-request-body-from-a-json-post-in-php) – Quentin Oct 06 '13 at 09:42
  • @Lizbeth please, write console.debug(type of dataStream); console.debug(dataStream); before $.ajax(...., cause dataStream looks like a json string, but I dont kow if you are pasting an object or dictionary, probably is a bad formed json – Carlos Oct 06 '13 at 09:52

2 Answers2

3

Change it as below:

data: {ds: JSON.stringify(dataStream) },

Then on PHP, you can get it by $_POST['ds']

Basically, data should be sent as key: value pairs

Rajesh
  • 3,743
  • 1
  • 24
  • 31
2

By using JSON.stringify, you are actually converting your Javascript object to a String. Thus, it will send a string to your PHP script, and you will need to json_decode() it to retrieve values.

You should rather use:

data: dataStream

to send an array of values, and retrieve them with $_POST['email'] and $_POST['skype'] in your PHP script.

Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42