简体   繁体   中英

JQuery - Bypass AJAX response once request is sent

Im using the following way, to request PHP page to send mail to many users. I don't need the response at all. What I want is to just Call the function and start doing my thing in JS.

$.ajax({
  type    : 'POST',
  url     : 'mail_users.php',
  data    : formData,
  dataType: 'JSON',
  encode  : true
})
.done(function(data) {
  console.log(data);
})
.fail(function() {
  console.log("ERROR");
});

I also just want to make sure the request is done properly. I tried many SO's suggestion like setTimeout() in JS and ignore_user_abort(true) , set_time_limit(0) in PHP.

But nothing worked for me,Is there any right snippet for this?. How can I achieve this? Thanks in advance.

Trying to help. I wrote a code which make a AJAX call to a URL. It contains the callback functions but you can ignore (as you don't wanna check the result). Please, try this code changing the target URL.

Result

Tue Jun 21 2016 15:46:30 GMT-0300 (BRT) Req sent {"email_id":"1","key":"abc"}
Tue Jun 21 2016 15:46:30 GMT-0300 (BRT) Success
Tue Jun 21 2016 15:46:30 GMT-0300 (BRT) Complete {"readyState":4,"responseText":"\n\t\n\t\t\n\t\t\n\t\n\t\n\t\tResult:\n\t\t
\n\t\n","status":200,"statusText":"OK"}

Code

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script>
            function log(msg) {
                var dateMsg = (new Date()) + ' ' + msg;
                var $res = $('#result');
                var $o = $('<li>').html(dateMsg);
                $res.append($o);
            }

            function checkEmailFrontEnd(email_id, key) {
                var userdata = {
                    email_id: email_id,
                    key: key
                };
                var opts = {
                    url: 'html.html',
                    type: 'POST',
                    data: userdata,
                    success: function(data, textStatus, jqXHR){
                        log('Success');
                    },
                    error: function(jqXHR, textStatus, error){
                        log('Error ' + error);
                    },
                    complete: function(jqXHR, textStatus){
                        log('Complete ' + JSON.stringify(jqXHR));
                    }
                };
                $.ajax(opts);   
                log('Req sent ' + JSON.stringify(userdata));
            }
            $(function(){
                checkEmailFrontEnd('1', 'abc');
            });
        </script>
    </head>
    <body>
        Result:
        <ul id='result'></ul>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM