I just ran into something weird. I have two JSON arrays which holds different data. Usually I just need data from one of them, but when I try to do a dual ajax call to get data from both arrays, I end up with the exact same data on both calls.
Say array 1 holds JSON data on users, and array 2 holds JSON data on houses, and I want to get data from both arrays: (PS! I cut out the url
and type
to save a couple of lines.)
$.ajax({
async:false,
data:"type=users&id=3,5,6",
success: function(data) {
data = JSON.parse(data);
alert(data.length) //will alert 3 as expected
}
});
Then I make the second call to get some houses in there as well:
$.ajax({
async:false,
data:"type=houses&id=2,4",
success: function(data) {
data = JSON.parse(data);
alert(data.length) //alerts 3 as well...
}
});
When I look at the params and response with Firebug, I can see that the params are correct, but the response is wrong.
In my PHP I even tried to just echo out whatever comes in doing this:
echo $_GET['id'] . ", " . $_GET['type'];
Which made the request look the exact same on both calls... If I put an alert inbetween the ajax calls, I get the expected result (since the system waits). But I thought putting them both to be synchronous would be enough to not crash up the calls..?
edit: I've tried creating a copy of the php file which is called within the AJAX function, and setting the calls to go to seperate files, which makes everything work as expected. So I'm fairly sure there's nothing wrong with the javascript.
more edit: If I remove the parameters from the second AJAX call, I still get the same result. Looking at the request with Firebug I can see that the params list is empty, but the response is still identical...
even more edit: Looking around in Firebug, I can see there is a header called connection
which has the value of keep-alive
, and then a header called Keep-Alive
with the value of 300
. I'm guessing this might have something to do with it? Can't find anything on it in the jQuery docs, though...
source code: I've made a small test case, which reproduces the problem:
PHP
echo $_GET['test'];
die();
HTML
<sript>
$(document).ready(function() {
$.ajax({
type:"get",
url:"bugtest.php",
data="test=hello",
success: function(data) {
$("output").append(data);
}
});
$.ajax({
type:"get",
url:"bugtest.php",
data="test=world!",
success: function(data) {
$("output").append(" "+data);
}
});
});
</script>
<h1>AJAX bug in Aptana's PHP server?</h1>
<output></output>
That's all I have, and it does the same thing: Instead of getting hello world!
like I'd expect, I get hello hello
...