I am trying to retrieve data for multiple IDs like this:
(Heavily simplified, but I hope I can make my point clear)
Controller:
var idList = [39,40,41];
$.each(idList, function(key, value){
var dataObject = {
type: "test",
id: value
};
var getData = DataFactory.get(dataObject);
getData.then(function(result){
console.log(result);
}
});
Factory:
app.factory("DataFactory", ['$http', '$rootScope',
function($http, $rootScope){
url = 'http://url/api';
var obj = {};
obj.get = function(object){
return $http({
method: 'GET',
params: object,
url: url
})
.then(function(results){
return results.data;
});
};
return obj;
}
]);
Backend:
<?php
$id = $_GET['id'];
$type = $_GET['type'];
echo json_encode(array("id": $id, "type": type, "value": "value matching id ".$id));
?>
If the idList is 1 integer, the data returned matches the id eg:
{id: 39, type: "test", value: "value matching id 39"}
However, when applying multiple values in the idList, the returned data is not applied to the correct id:
{id: 39: type: "test", value: "value matching id 41"}
{id: 40: type: "test", value: "value matching id 39"}
{id: 41: type: "test", value: "value matching id 40"}
I expected that if I'd send back the same ID, the value matching that ID would be correct. This is not the case. Is there any way I can properly bind the ID to the correct matching value?
Edit: Looking at the network tab in chrome the following happens:
For 1 id
url: api?id=39&type=test
result(preview): data: [id: 39, type: test, value: 'value matching id 39']
For 3 id's
url: api?id=39&type=test (same for 40 and 41)
result(preview): data: [id: 39, type: test, value: 'value matching id 40']
It almost looks like php is not handeling the requests properly. Opening the api url (http://url/api?id=39&type=test) always gives me the expected result. Calling the api multiple times from javascript gives me mixed up results.