Ajax POST internal error 500

0

Running the ajax throws the POST 500 error. The php throws the problem if I define $columna= $_POST['columna']; but if I define by test $columna='marca'; the PHP works correctly and sends the information to ajax correctly. So I guess the problem is in what Ajax sends and how PHP gets it.

function traer( id )
{
    $.ajax({
        url: '../server/info.php',
        dataType: "json",
        cache: false,
        processData: false,
        contentType: false,
        data: { id: id },
        type: 'POST',
        success: (data) =>{
            $( "#" + id ).html( data );
        },
        error: function(){
            alert( "Error con el servidor" );
        } 
    })
}

document.getElementById("marca").onclick=function(event){traer("marca");};
    
asked by jufrfa 26.07.2017 в 00:50
source

2 answers

4

If you want to receive a server-side parameter, you have to send it with the same name you expect in PHP.

In the ajax you have:

 $.ajax({
   data: {id: id}
   //..
 });

But in php you wait $_POST['columna']; .

For what should be:

 $.ajax({
   data: {columna: id}
   //..
 });

Remember that you can send whatever parameters you want:

 $.ajax({
   data: {columna: id, nombre__ : "Einer", sitio: "www.google.com" }
   //..
 });

And in the php:

$sitio = $_POST["sitio"];
$column = $_POST["columna"];
$nombre = $_POST["nombre__"];
    
answered by 26.07.2017 / 01:03
source
0

The error was caused by cache: false, processData: false, contentType: false, you just have to delete them. In addition, the data value is changed by data: "column=" + id, making PHP $ _POST ['column'].

function traer(id) {
 $.ajax({
   url: '../server/info.php',
   dataType: 'json',
   data: "columna="+id,
   type: 'POST',
   success:function(data) {
     $("#" + id).html(data);

   },
   error: function(){
     alert( "Error con el servidor" );
   }
 });

};

document.getElementById("marca").addEventListener("click", traer("marca"));
    
answered by 03.08.2017 в 20:45