AngularJS:同时$ http get在$ .each循环中返回错误的数据 [英] AngularJS: simultaneous $http get returns wrong data in $.each loop

查看:111
本文介绍了AngularJS:同时$ http get在$ .each循环中返回错误的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to retrieve data for multiple IDs like this:

(经过简化,但希望我能阐明我的观点)

(Heavily simplified, but I hope I can make my point clear)

控制器:

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为1整数,则返回的数据与id匹配,例如:

If the idList is 1 integer, the data returned matches the id eg:

{id: 39, type: "test", value: "value matching id 39"}

但是,在idList中应用多个值时,返回的数据不会应用于正确的ID:

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"}

我希望,如果我发回相同的ID,则与该ID匹配的值将是正确的.不是这种情况.有什么方法可以将ID正确绑定到正确的匹配值?

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?

查看chrome中的网络标签,会发生以下情况:

Looking at the network tab in chrome the following happens:

1个ID

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

对于3个ID

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网址( http://url/api?id = 39& type = test )总是给我预期的结果.从javascript多次调用该API会给我带来混乱的结果.

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.

推荐答案

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

In case you are having this problem in java when using spring configuration with jersey, the problem may be caused by the singleton nature of the resource when marked with @component. When a new request arrives, the same bean is used with the old injected values for query, header params, and other business objects. This means that it will fetch data using old values.

解决此问题的一种方法是,在每次有以下请求时,使用资源类上的原型范围来获取带有新注入的新实例:@Scope("prototype") 修改get方法签名,以便它需要的所有值(例如参数)都是在方法处注入的,而不是注入到对象中的.

One way to fix it is to use prototype scope on the resource class to get a new instance with fresh injection each time there is a request: @Scope("prototype") or to modify the get method signature so that all the values it needs such as parameters are injected at the method instead of into the object.

第二种方法更好,因为实例将被重用,并且每次方法调用仅更改参数.

The second option is better because the instance will be reused and only parameters will be changing for each method call.

代替此

@QueryParam("searchTerm")
private String searchTerm;

@GET
private Product findProduct() {
    productDao.find(searchTerm);
}

执行此操作

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

这篇关于AngularJS:同时$ http get在$ .each循环中返回错误的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆