Javascript AngularJS:Synchronized$http get在$中返回错误的数据。每个循环

Javascript AngularJS:Synchronized$http get在$中返回错误的数据。每个循环,javascript,php,jquery,angularjs,ajax,Javascript,Php,Jquery,Angularjs,Ajax,我正在尝试检索多个ID的数据,如下所示: url: url + "?id=" + object.id + "&type=" + object.type (高度简化,但我希望我能把我的观点说清楚) 控制器: var idList = [39,40,41]; $.each(idList, function(key, value){ var dataObject = { type: "test", id: value }; var getData = DataFa

我正在尝试检索多个ID的数据,如下所示:

url: url + "?id=" + object.id + "&type=" + object.type
(高度简化,但我希望我能把我的观点说清楚)

控制器:

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);
  }
});
工厂:

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;
  }
]);
后端:

<?php
  $id = $_GET['id'];
  $type = $_GET['type'];

  echo json_encode(array("id": $id, "type": type, "value": "value matching id ".$id));
?>
但是,在idList中应用多个值时,返回的数据不会应用于正确的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"}
我希望如果我发回相同的ID,匹配该ID的值将是正确的。事实并非如此。是否有任何方法可以将ID正确绑定到正确的匹配值

编辑: 查看chrome中的“网络”选项卡,会发生以下情况:

1号身份证

url: api?id=39&type=test
result(preview): data: [id: 39, type: test, value: 'value matching id 39']
3号身份证

url: api?id=39&type=test (same for 40 and 41)
result(preview): data: [id: 39, type: test, value: 'value matching id 40']

看起来php没有正确处理请求。打开api url()总是会得到预期的结果。从javascript多次调用api会产生混淆的结果。

看起来不错,可能是PHP没有识别“GET”操作

您是否尝试在工厂中放置console.log以查看是否使用了正确的url?(很高兴看到网络)

你也可以用“糟糕的方式”(只是为了测试),把url硬编码如下:

url: url + "?id=" + object.id + "&type=" + object.type

它看起来不错,可能是PHP没有识别“GET”操作

您是否尝试在工厂中放置console.log以查看是否使用了正确的url?(很高兴看到网络)

你也可以用“糟糕的方式”(只是为了测试),把url硬编码如下:

url: url + "?id=" + object.id + "&type=" + object.type

如果您在java中将spring配置用于jersey时遇到此问题,则该问题可能是由使用@component标记时资源的单例性质引起的。当一个新的请求到达时,同一个bean将与查询、头参数和其他业务对象的旧注入值一起使用。这意味着它将使用旧值获取数据

解决此问题的一种方法是在资源类上使用prototype作用域,以便在每次有请求时获得具有新注入的新实例:
@scope(“prototype”)
修改get方法签名,以便在方法中注入它所需的所有值,例如参数,而不是注入到对象中

第二个选项更好,因为实例将被重用,并且每个方法调用只更改参数

而不是这个

@QueryParam("searchTerm")
private String searchTerm;

@GET
private Product findProduct() {
    productDao.find(searchTerm);
}
@GET
private Product getProduct(@QueryParam("searchTerm") String searchTerm) {
    productDao.find(searchTerm);
}
这样做

@QueryParam("searchTerm")
private String searchTerm;

@GET
private Product findProduct() {
    productDao.find(searchTerm);
}
@GET
private Product getProduct(@QueryParam("searchTerm") String searchTerm) {
    productDao.find(searchTerm);
}

如果您在java中将spring配置用于jersey时遇到此问题,则该问题可能是由使用@component标记时资源的单例性质引起的。当一个新的请求到达时,同一个bean将与查询、头参数和其他业务对象的旧注入值一起使用。这意味着它将使用旧值获取数据

解决此问题的一种方法是在资源类上使用prototype作用域,以便在每次有请求时获得具有新注入的新实例:
@scope(“prototype”)
修改get方法签名,以便在方法中注入它所需的所有值,例如参数,而不是注入到对象中

第二个选项更好,因为实例将被重用,并且每个方法调用只更改参数

而不是这个

@QueryParam("searchTerm")
private String searchTerm;

@GET
private Product findProduct() {
    productDao.find(searchTerm);
}
@GET
private Product getProduct(@QueryParam("searchTerm") String searchTerm) {
    productDao.find(searchTerm);
}
这样做

@QueryParam("searchTerm")
private String searchTerm;

@GET
private Product findProduct() {
    productDao.find(searchTerm);
}
@GET
private Product getProduct(@QueryParam("searchTerm") String searchTerm) {
    productDao.find(searchTerm);
}

我试过你的建议,但结果都一样。我做了一个console.log(object)和console.log(results)(对于.get和results)对象具有所有正确的参数,但结果为该特定ID提供了不正确的值。php脚本基于该ID和ID本身返回一个值。因此,我无法理解为什么http.get的结果是错误的。而您得到了相同的结果?您可以在浏览器网络工具(我更喜欢chrome ou firefox)上检查请求。get URL是正确的(根据您的网络工具建议),单独使用组合ID+类型可以工作并返回预期的JSON。我也在chrome中的高级REST客户端扩展中尝试了这一点。这就是为什么我怀疑这与这些请求的异步性质有关。奇怪。从“网络工具”窗口打开api url时,我得到了与ID匹配的正确值。但是,在检查“预览”选项卡时,数据不正确。我尝试了您的建议,但结果相同。我做了一个console.log(object)和console.log(results)(对于.get和results)对象具有所有正确的参数,但结果为该特定ID提供了不正确的值。php脚本基于该ID和ID本身返回一个值。因此,我无法理解为什么http.get的结果是错误的。而您得到了相同的结果?您可以在浏览器网络工具(我更喜欢chrome ou firefox)上检查请求。get URL是正确的(根据您的网络工具建议),单独使用组合ID+类型可以工作并返回预期的JSON。我也在chrome中的高级REST客户端扩展中尝试了这一点。这就是为什么我怀疑这与这些请求的异步性质有关。奇怪。从“网络工具”窗口打开api url时,我得到了与ID匹配的正确值。但是,在检查“预览”选项卡时,数据不正确。现在,通过同步处理ID解决了此问题。我使用angular调用java api时曾两次遇到同样的问题。即使在使用Restanglar时也存在同样的问题。也许问题更根本。我只是为那些在使用spring和jersey时出现这种行为的人找到了答案。简单的解决方案是将资源类的作用域更改为原型,以便每个请求都有一个新的作用域。查看我下面的答案以了解更多细节。现在通过同步处理ID解决了这个问题。我曾经两次使用angular调用java api遇到同样的问题。即使在使用Restanglar时也存在同样的问题。也许问题更根本。我只是为那些在使用spring和jersey时出现这种行为的人找到了答案。简单的解决方案是更改资源类的