Problems with AJAX jQuery

-1

I have the following ajax and I want to send "data.php" to the data that my object obj has but it does not do it and it sends me the alert ("Unable to recover the data ...");

$.ajax({
       url: 'http://nombre_server/datos.php',
       method: 'POST',
       data: obj,
       success: function(data){
               console.log(data);
               alert(data);
       },
       error: function(data) {
              alert("No se pueden recuperar los datos...");
       }
});

the object obj I formed it in the following way

$("#lista div input, #lista div select").each(function(i,e){
                    obj.push({
                        id:$(this).attr("id"),
                        valor:$(this).val()
                    });
                });
                console.log(obj);

and printing the obj in console shows it in the following way

(10) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
0
:
Object
id
:
"0"
valor
:
"true"
__proto__
:
Object
1
:
Object
id
:
"1"
valor
:
"echo"

and in the file datos.php it shows me

undefined:
undefined:
undefined:
undefined: //son 10 undefined en total

The problem is that I do not know why ... I do not know what's wrong with someone who can help me.

    
asked by Soldier 31.08.2017 в 21:57
source

2 answers

0

Use in $.ajax :

data: {'data': obj}

and retrieve the data as $_POST['data']

To send an object in the ajax, but you could not recover it in PHP:

contentType: 'application/json',
data: JSON.stringify(obj),
    
answered by 01.09.2017 в 00:03
0

My friend the problem is that you should pay more attention as the ajax jquery works the data is sent a json that is an array constituted by property: value is that we send a name with which it will be received in the controller or file that processes the files

You, my friend, what you are doing is passing an array of ARRAY objects! but not a JSON that says the property: value.

an array has the positions [0], [1], [2] that are in your code being sent in a JSON array.

I hope you have explained to me if you want to send something through AJAX, make sure that you send a JSON through your httpRequest, that is, send only the JSON but not a JSON array.

What I personally suggest is to create a chain that you separate by a | ie id | value for example I create a string:

var string = $(this).attr('id')+"|"+$(this).val()

and that add it to an array called "miarray" then:

miarray.push(string);

then add that array of strings in the data as follows:

datos={'items[]':myarray};

Finally send data in your ajax like this:

$.ajax({
    url : 'mipagina.php',
    data:datos,
    method:'post',
    success: function(data){
           console.log(data);
           alert(data);
   },
   error: function(data) {
          alert("No se pueden recuperar los datos...");
   }});

and you receive in php with:

$POST_REQUEST['items[]'];

and nothing else you iterate the array again and you separate it with str_split or explode () in php I hope it serves you greetings !!!

    
answered by 01.09.2017 в 00:31