0

I am making an AJAX call but its not returning a value in the success handler. This is my AJAX call. I have checked that it's hitting the PHP file correctly

var msj;
$.ajax({
    type: "POST",
    url: "ajaxFile.php",
    data: {
        name: name,
        status: status,
        description: description,
        action: 1
    },
    sucess: function(data){
        msj = data;
        alert(data);
    }
});
alert(msj);

My PHP code is as follow:

if (isset($_POST['action']))
{
    if ($_POST['action'] == 1) 
    {
        $obj = new project($_POST['name'], $_POST['active'], $_POST['description']);
        $obj = testInput($obj);
        $check = validateName($obj->getName());
        if ($check == 1) 
        {
            echo $nameError;
        } 
        else 
        {
            print "asdasdasd";
        }
    }
}

Please help me tracking the mistake.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

2 Answers2

1

As far as I can see there's a syntax error in your code. There's sucess instead of success.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Gvidas
  • 1,964
  • 2
  • 14
  • 21
0

You are only providing a "success" callback function. You should also provide an "error" callback so you can debug and see what is wrong. You can also provide a "complete" callback that will be used in both alternatives.

var msj;
    $.ajax({
        type:"POST",
        url:"ajaxFile.php",
        data:{name:name,status:status,description:description,action:1},
        complete:function(data){
            msj=data;
            alert(data);
      }
    });
alert(msj);

In your PHP, you could add

header('Content-Type: application/json');

to make sure JQuery identify well your web service.

Orden
  • 589
  • 3
  • 13