0

I'm adding some custom JS to an application built in Wordpress to set appointments so that if an appointment request is made, both the client and worker are sent request emails. The appointment is made by pressing a button, and the request is made by binding the button to the .click() event.

To send the emails, I put the ajax function inside the success function of the first ajax function to approve the appointment, so that if the appointment was approved, the emails are sent. I was trying to set this up, but found that no matter what I did, the second ajax function inside the success function wouldn't fire, and it wouldn't even correctly report what the error was.

To test to see if this was the ajax function itself that was at fault, I took it out of the success function and put it within $(document).ready(function() { }), binding it to a different event to see if it would fire - and it did. So the function itself is not the problem, but rather that it is inside the success function of another ajax request.

What should I do to get this to fire? Other suggestions on Stack Overflow are very confusing at best, so please pardon me for asking again, but there doesn't seem to be a clear satisfactory answer to this.

This is the code that I put within the first ajax request's callback function, which worked perfectly well when I took it outside the callback function and bound it to a different eventy.

    $.ajax({  
              type: "POST",  
              dataType: "JSON", 
              url: "[home url]/wp-admin/admin-ajax.php",  
              data: "action=finchJsonTest",  
              success: process_lesson_request,  
              error: function(MLHttpRequest, textStatus, errorThrown){  
                        alert(errorThrown);
                        alert("Something\'s not right!");   
              }  
       });                                     

              function process_lesson_request(data) {
                          alert("Hooray!");  
                          console.log(data);                                               
              }  

And this is the php function that Wordpress calls to handle this ajax request (determined by the "action" variable in the data string) - there's nothing too important going on, I was just testing to see if it would successfully return a JSON object

function finchJsonTest() { 
    $json_array[] = 'This is something!'; 
    $finaljson_array = json_encode($json_array); 
    echo $finaljson_array; 
    die(); 

}  

add_action( 'wp_ajax_nopriv_finchJsonTest', 'finchJsonTest' );  
add_action( 'wp_ajax_finchJsonTest', 'finchJsonTest' );

Again, if I use this ajax call on its own bound to some event, it works perfectly and returns the desired JSON object (just text saying 'this is something'), but if I put this in the success callback function of another ajax request, it fails, and does not even throw what the error is. I have in fact read other Stack Overflow posts related to this, and wasn't able to make heads or tails of what to do. Any thoughts out there?

Nicholas Finch
  • 327
  • 1
  • 3
  • 11
  • Is the first AJAX call successful (returning a status code of 200)? You can use Firebug in Firefox (among other tools) to check the requests. – Vimal Stan Mar 25 '13 at 06:55
  • First is successful, it triggers a javascript alert that says "We have confirmed your appointment." I have added the second ajax request to take place after this. – Nicholas Finch Mar 25 '13 at 07:08
  • Also - I tried putting the request inside its own javascript function, and calling that function in the success callback, also does not work. – Nicholas Finch Mar 25 '13 at 07:08
  • Similar: [AJAX call inside AJAX call](http://stackoverflow.com/questions/10089447/jquery-ajax-request-inside-ajax-request). – Vimal Stan Mar 25 '13 at 07:43
  • That's exactly what I tried, and the ajax call works perfectly elsewhere, but not in the success function :( – Nicholas Finch Mar 25 '13 at 14:41
  • That's very odd, can you mock up a page on [JSFiddle](http://jsfiddle.net/) so that we can take a look? – Vimal Stan Mar 26 '13 at 04:56

0 Answers0