0
var myData = JSON.stringify($('form').serializeArray());

$.ajax({ 
    cache: false,
    url: "http://localhost/Demo/store.php",
    type: "POST",
    data: myData,           
    complete: function (xhr, status) {
        if (status === 'error' || !xhr.responseText) {
            alert(status);              
        }
        else {
            var r = xhr.responseText;                       
        }
    }
});

$decoded = json_decode($_REQUEST['myData'],true);
print_r($_REQUEST);
exit;

if (is_array($decoded))
{
    foreach ($decoded as $value) {
        echo $value["name"] . "=" . $value["value"];
    }
}

When i am trying to decode the data in php the error is undefined index myData..Please help me..Thanks.

som
  • 4,650
  • 2
  • 21
  • 36
yaseen cruise
  • 53
  • 2
  • 9
  • Am i using correct format to send the json string..?? In console log its showing the json string like this: [{"name":"name","value":"xyz"},{"name":"age","value":"22"},{"name":"gender","value":"male"},{"name":"skills","value":"Php"},{"name":"skills","value":"Java"},{"name":"note","value":"sadasd"}] – yaseen cruise Sep 03 '13 at 10:11
  • please edit your original post with additional information instead of posting it as a comment. – jdepypere Sep 03 '13 at 10:14
  • why do you want to JSON.stringify the value, try just with : var myData = $('form').serializeArray(); – RONE Sep 03 '13 at 10:15
  • possible duplicate of [How to retrieve Request Payload](http://stackoverflow.com/questions/9597052/how-to-retrieve-request-payload) – Quentin Sep 03 '13 at 10:15

5 Answers5

0

Give it a try with:

$decoded = json_decode($_POST['myData'],true);
Antoan Milkov
  • 2,152
  • 17
  • 30
0

Try this when call ajax

data: {'myData' : myData},

Then access using

json_decode($_POST['myData'],true); 

or

json_decode($_REQUEST['myData'],true);
som
  • 4,650
  • 2
  • 21
  • 36
  • Even if i declare an object and try to sue it with that i am able but your answer is more of an easy solution easy solution to this.. var param = new Object(); param['myData'] = myData; – yaseen cruise Sep 03 '13 at 10:35
  • Whne i try to decode json string its showing error as json_decode() expects parameter 1 to be string – yaseen cruise Sep 03 '13 at 10:40
0

try this pass datatype :

$.ajax({
    url: 'http://localhost/Demo/store.php',
    type: 'POST',
    dataType: 'json',
    data: $('#form').serialize(),
    success: function(xhr, status) {
    if (status === 'error' || !xhr.responseText) {
        alert(status);              
    }
    else {
        var r = xhr.responseText;                       
    }
}
});
Shakti Patel
  • 3,762
  • 4
  • 22
  • 29
-1

When you pass a string as the data property is just appends it as a parameter query string to the URL. If you want to send an encoded JSON string you still need to give it a name:

data: {myData: myData}

Now you can use the myData request parameter in your PHP script.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
-2

used dataType:json;tosend json.

Amnishyadav
  • 21
  • 1
  • 4