当在ajax中设置缓存为false时,它会在请求中添加一些值

时间:2018-06-13 04:10:24

标签: ajax

我使用了一小段Ajax代码,我的代码正在运行。我的代码中没有错误,但是当我在我的ajax中设置cache false时,它在request中添加了一些值。我想知道什么是值及其用途

我的代码是

function validate() {
    var user = $('#user').val();
    var num =  $('#num').val();
    var mobile=  $('#otp').val();
    $.ajax({
        type: "GET",
        url:  "/validateOtp",
        data: {user: user , num: num , mobile: mobile},
        dataType: 'text',
        cache: false,
        timeout: 600000,
        success : function(response) {
                alert( response );
            },
            error : function(xhr, status, error) {
                alert(xhr.responseText);
            }
    });
}

它在浏览器中生成这样的请求

http://localhost:8080/validateOtp?user=1234&num=12345&otp=1234&_=1528862398631

你可以看到ajax增加的值& _ = 1528862398631

我的后端代码在Spring MVC中

@Controller
@RequestMapping("/validateOtp")
public class ValidateOTPAjaxController {
private final Logger logger = 
 LogManager.getLogger(this.getClass().getSimpleName());

@Autowired
private OTPService otpService;

@RequestMapping(method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public String getAllDistrict(@RequestParam(value = "user") String user,
        @RequestParam(value = "num") String num, @RequestParam(value = "mobile") String mobile) {
    logger.debug(user);
    logger.debug(num);
    logger.debug(mobile);
    return "OK";
}

1 个答案:

答案 0 :(得分:0)

通过将缓存属性设置为false,jQuery将向URL附加时间戳,因此浏览器不会缓存它(因为URL对于每个请求都是唯一的。请参阅文档以获取详细信息:http://api.jquery.com/jQuery.ajax/

你的控制器应该如下:

@Controller
public class ValidateOTPAjaxController {
   private final Logger logger = 
   LogManager.getLogger(this.getClass().getSimpleName());

   @Autowired
   private OTPService otpService;

   @RequestMapping(value = "/validateOtp", method = RequestMethod.GET)
   public String getAllDistrict(@RequestParam("user") String user,
        @RequestParam("num") String num, @RequestParam("mobile") String mobile) {
      logger.debug(user);
      logger.debug(num);
      logger.debug(mobile);
      return "OK";
  }
}