search

Home  >  Q&A  >  body text

java - 浏览器发送了两次请求给服务器,服务器只返回后一次请求的内容

浏览器调用同一个接口,发送了两次请求给服务器端,服务器端代码返回一个随机数给浏览器

框架是SpringMvc+Spring+MyBatis

服务器端代码如下:

@RequestMapping(value = "/test")
@ResponseBody
public Result test(){
    result.setSuccess(true);
    result.setData(new Random().nextDouble() + System.currentTimeMillis());
    return result;
}

chorme的network截图如下,发现两次请求返回的内容是同一个

有以下几个疑问:


分割线1

不少朋友说是因为我发的get请求,导致浏览器认为是缓存原因。

我的实际应用场景是,前台上传多个附件,但是本质是多次上传,然后由后台返回此文件在数据库中的文件id。然后我发现有时上传多个文件时,返回的文件id都是同一个。

如下图所示:两个上传的文件长度是不一样的

但是服务器返回的文件id却是一样的:


分割线2

我已经在URL上加了随机数,来区分是不同的请求,服务器的响应码是200,但是服务器返回的内容还是将第二次请求的响应内容覆盖掉了第一次响应内容

PHPzPHPz2893 days ago553

reply all(6)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 09:46:59

    Try adding a timestamp to prevent caching after the URL requested by the browser

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:46:59

    After reading the comments, it may be that case. Take a look at the response code. If it is 304, it will be the same as last time. Because the requested URL is the same, you can add a random parameter (timestamp) to the request to cache the data used by the browser to ensure that the URL is inconsistent each time.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:46:59

    Look at the log of spring mvc controller to see if the time when the request reaches the server is the same millisecond

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:46:59

    Chief, you are here again, try using System.nanoTime() and see the return value.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:46:59

    Due to the lack of important information such as request data (is it a GET request or a POST request? How are these two requests initiated? How is the relevant client code written?), the following is only a personal guess:
    1. You two The requests were initiated to the server almost at the same time;
    2. Based on the guess of 1, new Random().nextDouble() + System.currentTimeMillis()It can almost be considered that the difference is not big (especially when testing locally);
    3. You used double to store this random number, and then jackson Precision loss may occur when serializing double values ​​(please search for the precision loss problem of js by yourself), so on the client side, your two values ​​​​will appear to be exactly the same;

    Based on the conjecture of 1, the hypothetical verification method is as follows:

    @RequestMapping(value = "/test")
    @ResponseBody
    public Result test(){
        result.setSuccess(true);
        result.setData(new Random().nextDouble() + System.currentTimeMillis());
        log.info(result.getData());
        return result;
    }

    Look at the output results, and use JSON.parse (the value logged) in the console of the browser to see if they are the same.

    Solution: Use a string to store this random number. The following is a method I wrote myself to generate several digits of random numbers (java8):

    public static String generateRandomNum(int size) {
        return new Random().ints(size, 0, 10).boxed().map(String::valueOf).collect(Collectors.joining(""));
    }

    As for the problem after your dividing line, there is no code and it cannot be analyzed.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:46:59

    On the server side, print the requested URL path parameters to the console. The two requests printed to the server should be exactly the same, so the server handles them as one request. You can try the above System.nanoTime(); method.

    reply
    0
  • Cancelreply