http request inside another http request success response not working in jquery

for the purpose of modifying a wordpress plugin to send sms notification i am trying to make an http request using jquery.post() event inside success response of another http request, but 2nd http request not working, it doesn’t return any response, what’s wrong with my code?

//first http request

jQuery.post(redi_restaurant_reservation.ajaxurl, data, function (response) {
    jQuery('#redi-restaurant-step3').attr('disabled', false);
    jQuery('#step3load').hide();
    if (response['Error']) {
        jQuery('#step3errors').html(response['Error']).show('slow');
    } else {
        var smsurl = 'http://sms.mysitename.com/SendSms.aspx?uid=myuserId&pass=123&contact=phone&sms=smstext&rnd=randomnumber';
        alert(smsurl);
        //

        //2nd http request. problem arises from here............

        jQuery.post(smsurl, function (a) {

        alert('hello'); //not show any alert here

        if (a['Error']) { 
            alert('if');
            jQuery('#step3errors').html(a['Error']).show('slow');
        } else {

            alert('successfully sent');
        } 
         }, 'json');

        ga_event('Reservation confirmed', '');
        jQuery('#step1').hide('slow');
        jQuery('#step2').hide('slow');
        jQuery('#step3').hide('slow');
        jQuery('#step4').show('slow'); //success message
        jQuery('html, body').animate({scrollTop: 0}, 'slow');
    }
}, 'json');

Related posts

2 comments

  1. in my case all the trouble belong to this line-

     var smsurl = 'http://sms.mysitename.com/SendSms.aspx?uid=myuserId&pass=123&contact=phone&sms=smstext&rnd=randomnumber';
    

    jQuery.post() event can’t directly working with this url,

    so i modified my code like below and it works like charm!!

                    var msg = encodeURIComponent("Your reservation is confirmed! Thank you for your reservation");
                    var smsurl = "http://sms.mysitename.com/SendSms.aspx?uid=myuserId&pass=123&contact="+userPhone+"&sms="+msg+"&rnd=123";
                    var data = {url: smsurl};
    
                    jQuery.post('http://localhost/booking/wp-admin/sendsms.php', data, function (response) {
    
                        if (response['Error']) {
                        jQuery('#step3errors').html(response['Error']).show('slow');
                        } else {
                            ga_event('Reservation confirmed', '');
                            jQuery('#step1').hide('slow');
                            jQuery('#step2').hide('slow');
                            jQuery('#step3').hide('slow');
                            jQuery('#step4').show('slow'); //success message
                            jQuery('html, body').animate({scrollTop: 0}, 'slow');
                        }
    
                    });
    

    and my sendsms.php performs CURL request and return my desired response ..

    <?php
    $ch = curl_init();
    $the_url = $_POST['url'];
    curl_setopt($ch, CURLOPT_URL, $the_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result; //die();
    ?>
    

    hope this helps another. 🙂

Comments are closed.