0

I am trying to do a connection check for a form before it is submitted, when the button is clicked on. The idea is to look through a Timeout request "Timeout()..." which is not implemented yet in this code, and keep iterating through the Ajax until a connection is found, as sometimes out in the field the connection can drop. When a connection is found it will alert the user and will submit successfully if there was a dropped connection after hitting submit. Here is what I have so far:

function upload_prepformDiff() {
$.ajax({
        type: 'POST',
        url: './php/upload_prepform.php',
        timeout: 2000, //2 seconds, for testing purposes
        data: prepform,
        async: false,//Omitted now as of this post
        dataType: 'text',
        success: function() {
            alert("Your Prep form has been submitted.");
            window.top.location.replace('./');
        },

        error: function (xhr, status, error) {
            if(status == "timeout") {
                alert("Internet connection has been lost! Please wait until you are notified and do not continue.");
            } else {
                alert(status + " " + error);
            }
        }
});
};

The issue is that even with a low timeout value, I do not get a "timeout" status message I get "error." So it never throws the timeout error I need and for error I get: Error: NETWORK_ERR: XMLHttpRequest Exception 101

So the ajax does notice there is no connection, but that is what the errorThrown shows, while the textStatus is "error" for (xhr, status, error) respectively. So what I TRIED doing was do a little improvising and do some type of error.indexOf() deal with the error string thrown in the Ajax, but that didn't work nor did error.contains("NETWORK_ERR") or any type of Regex command. Any ideas for improving this or why I am not getting a timeout? Thanks!

4
  • 1
    Why use async: false ? Also, are you sure the error callback is triggered by the ajax request timing out, or perhaps there is an error server side? Commented Jun 27, 2013 at 21:57
  • ah, i must have changed something last second before posting and forgot to change back. As for async false, I do that because I am using global variables like //Global variables var prepform = { 'cable_no' : "", 'prep_date' : "", 'co_name' : "" } and then iterate through those global variables in the HTML by for (eID in prepform) { prepform[eID] = document.getElementById(eID).value; //alert(prepform[eID]+': '+document.getElementById(eID).value); } and async: false was only way for the ajax to fire off correctly to pass all that data through. Commented Jun 27, 2013 at 22:00
  • 1
    @jflay: That's no reason for using async: false. It's entirely possible to do async-enabled loops. Commented Jun 27, 2013 at 22:01
  • @BradM I also get a success and upload to my SQL database with a connection, so no errors there but only when I don't have any connection do I get the error, and I expect a "timout" but I get "error." I do have PHP being called for the Ajax request so I maybe since the Ajax can't access that PHP file in the Ajax URL request, it is throwing error? I don't know how to change that though... I just want some way to tell the user they have no connection. I am getting a correct error message, but don't know what the status is... Commented Jun 27, 2013 at 22:03

1 Answer 1

2

Guess this might be a bit late for you, but nevertheless...

If you have specified

async as 'false'

, the timeout property will be ignored.

As for handling the errors, you can visit an earlier SO question: status of ajax or post request

Hope this helps! :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.