0

I have code that looks something like this:

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
        function myButton_click(){
            alert('got here');
            $.get("myPage.cfm", function(data){alert('load was performed')});
        }
    </script>
</head>
<body>
    <input type="button" id="myButton" value="My Button" onClick="myButton_click()" />
</body>
</head>

The first alert ("got here") shows up, but the "load was performed" doesn't. Is the get being executed? What am I doing wrong?

Edit:

I've tried it with "http://www.google.com" instead of "myPage.cfm" to make sure it's not a problem with my page and it's still not alerting...

froadie
  • 79,995
  • 75
  • 166
  • 235

2 Answers2

1

According to the docs, the function you pass as the second parameter to the $.get call is only called on success. So if your server is returning any kind of error (400, 403, 404, 500 etc.), that function will not be called. If you want a function to be called no matter what, use complete:

$.get("myPage.cfm").complete(function(jqXHR, textStatus) {
  alert("Load was performed");
});
Felix
  • 88,392
  • 43
  • 149
  • 167
  • ok, got it. using the network tab (thanks boldewyn :) ), I can see that the page is returning an error. – froadie Nov 14 '11 at 16:58
0

The callback only executes on success (Http status code in the 200s or 304), try using the longhand jQuery.ajax method and following the advice in this SO question for how to handle this response in a reasonable manner.

Community
  • 1
  • 1
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • 2
    Actually that's slightly incomplete, jQuery considers these status codes "successful" `status >= 200 && status < 300 || status === 304` – aziz punjani Nov 14 '11 at 16:54