The jquery .ajax() request returns nothing on the first attempt. Only if I fire the same function again does it return the correct data.
Here is the code I use to solve the issue (still the cause is unknown):
$(document).ready(function(){
$(window).load(function(){
$('input').change(function() {
$('#b').removeClass("valid");
if ($('#b').val()) {
bajax(false);
}
}
function bajax(e) {
$.ajax({
type: "GET",
dataType: 'text',
data: 'i='+$('#b').val(),
cache: false,
url: 'b.php',
success: function(data){
$('#b').val(data);
$('#b').addClass("valid");
},
error: function() {
if (!e) {
bajax(true);
} else {
$('#b').val("An error occured.");
$('#b').removeClass("valid");
}
}
});
});
});
});
#b
is a simple <input>
, and b.php
outputs a short string. I am testing it right now with echo "123";
and as I said it works, except that I have to fire this request twice to get the text to show up.
EDIT: console.log output:
[16:47:54.893] "Running..."
[16:47:55.096] GET b.php?i=19&_=1388245674895 [0ms]
[16:47:55.583] "Call to error function"
[16:47:59.595] GET b.php?i=19&_=1388245679534 [0ms]
[16:47:59.533] "Running..."
[16:48:00.025] "Came into the success function with" "123"