java - ajax成功到后臺不知道為什么一直回調失敗函數
問題描述
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:請求成功有數據返回,很大可能與你的返回數據格式不對有關系。因為你設置了dataType : ’json’ 預期服務器返回的數據類型。這樣往往會進入 error 回調。你排除一下返回數據。
而且,error是有三個回調參數的,請自行打印出來。
ajax 跳入error的一些原因
回答2:彈出你的返回值,看看數據就知道了
回答3:HttpServletResponse和ajax的回調沖突了,去掉HttpServletResponse就行。
回答4:看到你的 dataType : ’json’, 要求的是服務器返回json格式,倘若服務器返回的數據不是json格式的數據,則會走進失敗的回調中。
回答5:將你AJAX配置dataType:'text',然后用alert(data)查看返回值
由于Ajax請求和response不一樣,得到數據后頁面不需要再渲染,所以不需要RESPONSE跳轉到新頁面。所以不需要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:沒注意這個ajax是跨域請求的 。
回答7:你的返回值數據類型是json,你后臺卻給他返回了一個Map,把你的map轉成json
相關文章:
1. 致命錯誤: Class ’appfacadeTest’ not found2. html5 - 如何實現帶陰影的不規則容器?3. objective-c - iOS開發支付寶和微信支付完成為什么跳轉到了之前開發的一個app?4. css - 移動端字體設置問題5. python - 管道符和ssh傳文件6. javascript - 循環嵌套多個promise應該如何實現?7. mysql優化 - 關于mysql分區8. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應9. 前端 - IE9 css兼容問題10. javascript - ionic2 input autofocus 電腦成功,iOS手機鍵盤不彈出
