0

im trying to send a array from javascript to php using ajax. My problem is that array in PHP is empty and i dont know why.

My array: vacio['num1:1','num2:2'....]

this my javasvascript:

    var vacio = new Array; 
    vacio = addElementAttributesToArrayEncode();

    var miJSON = JSON.stringify(vacio);

    var miAjax = new Request({
        type: "POST",
        url: "/multyWeb/actions/maintenance/mUpdateProductAction.php",
        data: "ark=" + miJSON,
         onSuccess: function(textoRespuesta){
        console.log("ok");console.log(textoRespuesta);
        },
        onFailure: function(){
        console.log("fallo");
        }
})
miAjax.send();

And this is my php:

if($_POST){
        echo "recibo algo POST Producto";
        //recibo los datos y los decodifico con PHP
        $str = json_decode($_POST['ark'], true); 
        echo json_encode($str); }

        else { echo "Error del POST";}

And the array $str is empty... what is wrong?

thanks

suaaa
  • 11
  • 3

2 Answers2

2

modify your ajax call and set the content type to json and send the data as json instead of stringify it.

var miAjax = new Request({
    type: "POST",
    url: "/multyWeb/actions/maintenance/mUpdateProductAction.php",
    contentType: "application/json"
    data: {'data':miJSON},
     onSuccess: function(textoRespuesta){
    console.log("ok");console.log(textoRespuesta);
    },
    onFailure: function(){
    console.log("fallo");
    }
})
miAjax.send();
Ashish Patel
  • 921
  • 1
  • 10
  • 26
0

It should be used in the first example jQuery

$.post('/multyWeb/actions/maintenance/mUpdateProductAction.php', {'param':'value','param1':'value1'}, function(data){
    return;
});

OR

var vacio = new Array; 
vacio = addElementAttributesToArrayEncode();

var miJSON = JSON.stringify(vacio);

var miAjax = new Request({
    type: "POST",
    url: "/multyWeb/actions/maintenance/mUpdateProductAction.php",
    data: {'ark':miJSON},
     onSuccess: function(textoRespuesta){
    console.log("ok");console.log(textoRespuesta);
    },
    onFailure: function(){
    console.log("fallo");
    }
})
miAjax.send();
Deniz Aktürk
  • 362
  • 1
  • 9