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

1

2 Answers 2

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();
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();

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.