我使用 Ajax 的一小段代码并且我的代码正在工作。我的代码中没有错误,但是当我在我的 ajax 中设置缓存 false 时,它会在请求中添加一些值。我想知道该值是什么及其目的。
我的代码是
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";
}
通过将缓存属性设置为 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";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句