开发者

Ajax file stops executing

I have this snippet of AJAX code which is working correctly in the fact its updating my database, only once its done its not executing the rest of my code...

$(".getPoint").click(function()
{        
   var theid = $(this).attr("id");
   var onlyID = theid.split("_");
   var onlyID = onlyID[1];
   var credoff = parseInt($(this).children('input.credoff:hidden').val());

    $.ajax({            
      url: 'do.php',
      type: 'POST',          
      data: "userID=" + onlyID + "&credoff=" + credoff,
      success: function(data) {
          if(data != "success1" && data != "success5") {
               $("#" + theid).text(data);  
          }else{

              $("#thediv_" + onlyID).fadeOut("slow");
              $('#creditsBalance').fadeOut("slow");
              newbalance = parseInt($('#creditsBalance').text());

          if(data != "success5") { 
               newbalance = newbalance+credoff;
              }else{
                newbalance = newbalance+5;
              }
          alert ('hi');

              $('#creditsBalance').text(newbalance);
              $('#creditsBalance').fadeIn("slow");
              $("#" + theid).text("Done"); 
          }
      },
      beforeSend: function()
      {
            $("#" + theid).text("Working...");                  
      },
      error: function()
      {           
        $("#" + theid).text("Failed...Click to Retry");           
      }          
 开发者_开发知识库   });
});

The line

if(data != "success5") { 
    newbalance = newbalance+credoff;
}else{
    newbalance = newbalance+5;
}
alert ('hi');

Updates my DB but then I dont receive an alert, Is this enough code for anybody to see where im going wrong?


Your server may be adding newlines or other spacing which screws up string comparisons, be sure to trim whitespace before doing such operations:

success: function(data) {
  data = data.trim();


if you want to send the alert on success change your snippet like this

if(data != "success5") { 
    newbalance = newbalance+credoff;
}else{
    newbalance = newbalance+5;
    alert ('hi');
}

this way if it is 'success5' you will get your alert


That line doesn't update the database at all. The php code that updates the database is already executed. You'll need to find out where the point is reached where the if begins. If it isn't, the problem could be in the php code (after the part where the database is updated) or it could be in the javascript code, either in specifying the correct OnSuccess event handler for the AJAX request, or in the actual code in that event handler.

In this case, it could very well be that newbalance is not defined inside the event handler, so executing the line

newbalance = newbalance + whatever;

will cause an error.

Easiest thing to try: put an alert('yoyoyo'); at the top of the success handler. If you don't get that message, the event is not called.

Make sure you test using FireBug or a similar tool, to check if you don't get any syntax errors or other errors. If you don't and you don't get the message, the event handler is not called and the problem probably lies in the server code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜