javascript - ajax后臺(tái)跳轉(zhuǎn)到了其他頁面,還報(bào)錯(cuò)
問題描述
ajax執(zhí)行后跳轉(zhuǎn)到后臺(tái)處理,處理完卻跳轉(zhuǎn)到了其他頁面。
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 class='get-code'>測(cè)試</button>
后臺(tái)
@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('隨機(jī)數(shù)為:'+result); session.setAttribute('result', result+''); return null;}
控制有輸出,說明進(jìn)來了,
最后卻跳轉(zhuǎn)到了其他頁面
控制臺(tái)還報(bào)錯(cuò)了
問題解答
回答1:簡(jiǎn)單的看了下,你后臺(tái)的隨機(jī)數(shù)已經(jīng)打出來了,說明Controller層執(zhí)行了。你既然用@ResponseBody了,為什么不直接返回你的隨機(jī)數(shù),result! 其次,根據(jù)你的回答,處理完卻跳轉(zhuǎn)到了其他頁面。 這個(gè)問題,應(yīng)該不會(huì)跳轉(zhuǎn)的,因?yàn)槌晒φ?qǐng)求后端數(shù)據(jù)拿到,你應(yīng)該在ajax的success里,獲取你要的數(shù)據(jù),即result。事實(shí)上你的success里什么也沒執(zhí)行啊!個(gè)人理解^~^
回答2:謝謝大家的答案, 由于是svn管理項(xiàng)目,昨天同事不小心刪了同頁面的一個(gè)form表單的</form>,導(dǎo)致出現(xiàn)的問題
回答3:你為什么把生成的驗(yàn)證碼放session里,直接把數(shù)據(jù)返回給那個(gè)ajax方法不就行了嗎?ajax里success方法會(huì)接收到返回?cái)?shù)據(jù)的。
相關(guān)文章:
