After ajax is executed, it jumps to background processing, but after processing, it jumps to other pages.
ajax:
$(".get-code").click(function(){
var user_number=$("input[name=user_number]").val();
alert(user_number);
$.ajax({type : "post",
url : "user/validate",
dataType : "json",
data : {"phone" : user_number},
success : function(data) {
}
});
});
Button
<button class="get-code">测试</button>
Backstage
@ResponseBody
@RequestMapping("/validate")
public Object validate(HttpSession session,String phone){
Integer result1=(int)((Math.random()*9+1)*100000);
String str=Integer.toString(result1, 10);
String result= str; /*Sampler.samlerInfo(phone, null);*/
System.out.println("随机数为:"+result);
session.setAttribute("result", result+"");
return null;
}
The control has output, indicating that it has come in,
In the end it jumped to other pages
The console also reported an error
After a brief look, the random numbers in your background have been printed, indicating that the Controller layer has been executed. Since you used @ResponseBody, why not just return your random number, result! Secondly, according to your answer, it jumps to another page after processing. This question should not jump, because after successfully requesting the back-end data, you should get the data you want, that is, the result in the ajax success. In fact, nothing is executed in your success! Personal understanding^~^
Thank you for your answers. Since it is an SVN management project, my colleague accidentally deleted the </form> of a form on the same page yesterday, which caused the problem
Why do you put the generated verification code in the session? Isn’t it enough to directly return the data to the ajax method? The success method in ajax will receive the return data.