error in ajax

I am trying to use ajax to call to my database to return in json format to populate a second box based on the selection from a first one.
If my database fails for some reason, nothing gets returned or sends back a 500 error.
I would like to run something else if the ajax fails.
right now, what is in the error section does not happen.
          $.ajax({
                type: "GET",
                url: "scripts/getmodels.asp",
                data: { name: $('#make').val() },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#model").get(0).options.length = 0;
                    $("#model").get(0).options[0] = new Option("Select a model", "");
                    $.each(msg.modelList, function (index, item) {                     
					   $("#model").get(0).options[$("#model").get(0).options.length] = new Option(item.optionDisplay, item.optionValue);
                 });
               },
                error: function () {
                  	ifExistsCss('modelboxcar',"invisible" );
					ifExistsCss('modelboxblank',"show" );
                }
            });//end ajax

Open in new window

rivkamakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
Did you check fiddler if request is sent to server?
0
rivkamakAuthor Commented:
yes, and it's coming back empty
0
Julian HansenCommented:
Some questions

Why would your database fail? It sounds like it is happening frequently?

If you want to catch a 500 error try this
$.ajax({
    ...
    statusCode: {
        500: function() {
          // your code here
        }
      }
   });

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

rivkamakAuthor Commented:
My database doesn't fail often, but it's very important to have a backup policy when it doesn
0
Najam UddinCommented:
what is response code you get in such situation? Does a simple alert works on fail?
0
Julian HansenCommented:
Did you try this?
$.ajax({
    ...
    statusCode: {
        500: function() {
          // your code here
        }
      }
   });

                                          

Open in new window

0
rivkamakAuthor Commented:
Did I put it in the wrong place?
it's throwing an error
	            $.ajax({
                type: "GET",
                url: "//www.kars4kids.org/scripts/getmodels.asp",
                data: { name: $('#make').val() },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#model").get(0).options.length = 0;
                    $("#model").get(0).options[0] = new Option("Select a model", "");
                    $.each(msg.modelList, function (index, item) {                     
					   $("#model").get(0).options[$("#model").get(0).options.length] = new Option(item.optionDisplay, item.optionValue);
                 });
               },
                error: function () {
                  	ifExistsCss('modelboxcar',"invisible" );
					ifExistsCss('modelboxblank',"show" );
                }
					, statusCode: {
        500: function() {
        	ifExistsCss('modelboxcar',"invisible" );
					ifExistsCss('modelboxblank',"show" );
        }
				}
				
            });//end ajax

Open in new window

0
Julian HansenCommented:
Here is some sample code that demonstrates the concept.
HTML
<a href="reflect.php">Valid URL</a><br/>
<a href="wrongurl">Bad URL</a><br/>
<a href="thispagedoesnotexist.html">URL not exist</a><br/>
<a href="error500.php">500 Error</a>

Open in new window

JQuery
<script>
$(function() {
  $('a').click(function(e) {
    e.preventDefault();
    $.ajax({
      url: $(this).attr('href'),
      success: function(response) {
        alert('success');
      },
      error: function(resp) {
        alert('error');
      },
      statusCode: {
        404: function() {
          alert('URL not found');
        },
        500: function() {
          alert('Error 500');
        }
      }
    });
  });
});
</script>

Open in new window

Working sample here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rivkamakAuthor Commented:
I  just realized that your code works in firefox.
In chrome I just get a message saying stalled.
i tried adding timout:5000,
but that didn't help.
Any ideas?
0
Julian HansenCommented:
I just re-tested in Chrome, IE 11 even tried on IE8 - worked in all of them
0
rivkamakAuthor Commented:
now, it's not throwing a 500 error.
over the weekend, I forced the page to fail. In firefox, i get the 500 error and it throws the right script.
In chrome, it doesn't hit 500, it just says stalled.
0
Julian HansenCommented:
Your script or the sample?

Sample works for me - I am forcing the 500 using a headers statement that returns a 500.

Still can't replicate the issue here.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.