Reputation: 2029
I was coding an usual jquery+AJAX code, but it isn't working and i really don't gettinh why! This is the part of my code that isn't working properly:
var data = $("#data_res").val();
var infoData = data.split("/");
var dataDb = infoData[2]+"-"+infoData[1]+"-"+infoData[0];
$.ajax({
type: "POST",
url: "reserva_ajax.php",
data: {action: 1, data: dataDb},
success: function(retorno)
{
var qtd = parseInt(retorno, 10);
alert("Qtd: "+qtd);
if(qtd > 0) {
//for now, qtd is always 0
}
else{
var id = $("#id_func").val();
var obs = $("#obs_res").val();
alert("id: "+id+" | obs: "+obs+" | dataDB: "+dataDb);
$.ajax({
type: "POST",
url: "reserva_ajax.php",
data: {action: 1, data: dataDb},
succes: function(a)
{
alert("sucess! a="+a);
},
error: function(b)
{
alert("Error!");
});
alert("passed =/");
}
},
error: function()
{
alert("Erro! Try again.");
}
Maybe the fact of being an ajax request inside another ajax request do the error. But I think that it's not the problem, since the ajax only is executed after the first one has finished.
The first ajax run ok, then the "Qtd: 0" is shown. Then the code goes to the else condition (since qtd isn't greater than 0), the second alert is shown: alert("id: "+id+" | obs: "+obs+" | dataDB: "+dataDb); everything OK.
But then, I get the problem! The ajax is executed (I think) but it doesn't execute the success block neither the error block, so the alert printed is: alert("passed =/");
I know that the ajax is asynchronous and the success and error alert probably will be executed after "passed" one, but they are never executed.
Please someone can help me?!
The php code is that:
<?php
include_once 'classes/reserva_class.php';
if(isset($_POST["action"]))
{
$action= (int) $_POST["action"];
switch($action)
{
case 1:
echo Reserva::getQtdReservasData($_POST["data"]);
break;
case 2:
echo "=)";
break;
}
}
?>
Upvotes: 0
Views: 198
Reputation: 7735
i think you misstyped one of your ajax options : type "Success" instead of "succes"
Upvotes: 1
Reputation: 21969
You're missing a closing }
on your error
action callback, and your success
callback is actually called succes
. It should look like this:
$.ajax({
type: "POST",
url: "reserva_ajax.php",
data: {action: 1, data: dataDb},
success: function(a)
{
alert("sucess! a="+a);
},
error: function(b)
{
alert("Error!");
}
});
By the way, you shouldn't 'think' that your ajax request is being executed... Pop open Opera Dragonfly or WebKit's Web Inspector or whatever developer tools of whatever browser you're using and hit the 'Network' tab, you will then be able to see every request that is made, and ensure that 2 are being sent.
Upvotes: 1
Reputation: 73
It looks like you're missing a closing curly brace on your "error" event on the second AJAX call.
If that doesn't fix it, try adding the "Complete" event to see what your ajax page is returning, if you're getting nothing back from "Error" or "Success".
Upvotes: 1