Situation:
- I have a simple form and will call a
.post()
-call (ajax) instead of the actual submit. - The post works as such that it will post the request and receive a
200 (OK)
status with the expected string in the response body (as can be seen in the Net-View of FireBug).
Question:
- The anonymous function with the
alert()
is never executed. Why is that?
Notes:
- I have inserted an alert to indicate when the method fires
- fiddle-links for convenience below the code.
The Code:
Here is my form:
<form>
<input type="submit" value="Submit" />
</form>
This was what I expected to work:
$(document).ready(function () {
$('form').submit(function (e) {
e.preventDefault();
$.post('/echo/html/', {
html: "<span>jsFiddle test return msg</span>",
delay: 1
}, function (data, textStatus, jqXHR) {
$('form').append(data);
alert("done"); // <----- THIS NEVER FIRES
}, 'json');
});
});
This does neither work:
$(document).ready(function () {
$('form').submit(function (e) {
e.preventDefault();
$.post('/echo/html/', {
html: "<span>jsFiddle test return msg</span>",
delay: 1
}, null, 'json').done(function (data, textStatus, jqXHR) {
$('form').append(data);
alert("done"); // <----- THIS NEVER FIRES
});
});
});
This will trigger the alert but data will be empty.
$(document).ready(function () {
$('form').submit(function (e) {
e.preventDefault();
$.post('/echo/html/', {
html: "<span>jsFiddle test return msg</span>",
delay: 1,
success: function (data, textStatus, jqXHR) {
$('form').append(data); // <----- DATA IS UNDEFINED
alert("done"); // <----- THIS FIRES!
}
}, null, 'json');
});
});