java - ajax成功到后臺(tái)不知道為什么一直回調(diào)失敗函數(shù)
問題描述
function a() {$.ajax({url : 'http://localhost:8080/ubi/checkIntegral',async : true,data:{'carOwnerID':'111111'},dataType : ’json’,type : ’GET’,success : function() { alert('ss');},error : function(map){ alert('FALSE');} });}@RequestMapping(value='/checkIntegral',method = RequestMethod.GET)@ResponseBodypublic Map<String,Long> checkIntegral(@RequestParam String carOwnerID ,HttpServletRequest request,HttpServletResponse response){ Long integral = impl.checkIntegral(Long.valueOf(carOwnerID)); Map<String,Long> map = new HashMap<String, Long>(); map.put('msg', integral); return map;}
問題解答
回答1:請求成功有數(shù)據(jù)返回,很大可能與你的返回?cái)?shù)據(jù)格式不對有關(guān)系。因?yàn)槟阍O(shè)置了dataType : ’json’ 預(yù)期服務(wù)器返回的數(shù)據(jù)類型。這樣往往會(huì)進(jìn)入 error 回調(diào)。你排除一下返回?cái)?shù)據(jù)。
而且,error是有三個(gè)回調(diào)參數(shù)的,請自行打印出來。
ajax 跳入error的一些原因
回答2:彈出你的返回值,看看數(shù)據(jù)就知道了
回答3:HttpServletResponse和ajax的回調(diào)沖突了,去掉HttpServletResponse就行。
回答4:看到你的 dataType : ’json’, 要求的是服務(wù)器返回json格式,倘若服務(wù)器返回的數(shù)據(jù)不是json格式的數(shù)據(jù),則會(huì)走進(jìn)失敗的回調(diào)中。
回答5:將你AJAX配置dataType:'text',然后用alert(data)查看返回值
由于Ajax請求和response不一樣,得到數(shù)據(jù)后頁面不需要再渲染,所以不需要RESPONSE跳轉(zhuǎn)到新頁面。所以不需要RETURN,而是通過PrintWriter打印到請求的頁面@RequestMapping(value='/checkIntegral',method = RequestMethod.GET)@ResponseBodypublic void checkIntegral(@RequestParam String carOwnerID ,HttpServletRequest request,HttpServletResponse response){
Long integral = impl.checkIntegral(Long.valueOf(carOwnerID)); PrintWriter writer=response.getWriter(); writer.write(String.valueOf(integral)); writer.flush(); writer.close();
}
回答6:沒注意這個(gè)ajax是跨域請求的 。
回答7:你的返回值數(shù)據(jù)類型是json,你后臺(tái)卻給他返回了一個(gè)Map,把你的map轉(zhuǎn)成json
相關(guān)文章:
1. button按鈕點(diǎn)擊了沒有任何反應(yīng)2. mysql - redis和mongodb怎么結(jié)合3. ddos - apache日志很多其它網(wǎng)址,什么情況?4. php安裝lpsolve 擴(kuò)展求助5. pbootcms程序的詳情頁模板想要實(shí)現(xiàn)多版塊展現(xiàn),說不清楚,我截圖在里面6. JSP頁面導(dǎo)入問題類文件放在WEB-INF / classes中的包中7. html5和Flash對抗是什么情況?8. python - type函數(shù)問題9. 第6行的$_GET[’F’]沒有定義啊?求問一下是什么意思?10. index.php錯(cuò)誤,求指點(diǎn)
