search

Home  >  Q&A  >  body text

javascript - ajax background jumps to other pages and reports an error

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

扔个三星炸死你扔个三星炸死你2719 days ago835

reply all(3)I'll reply

  • 代言

    代言2017-06-20 10:07:46

    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^~^

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-20 10:07:46

    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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-20 10:07:46

    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.

    reply
    0
  • Cancelreply