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. linux - python 安裝 Anaconda 環境變量問題請教2. javascript - node中為中間層如何解決跨域問題3. javascript - 請教一個問題,大家都是怎么安裝從github下面clone下來的包的開發環境呢?4. android - As ddms報錯5. 看了好幾遍為什么點擊登錄沒有反應呢 在線等。。。。6. 在mac下出現了兩個docker環境7. html5 - 百度echart官網下載的地圖json數據亂碼8. PhpStudy 8.0 一個服務器怎么創建多個網站,需要注意一些什么9. javascript - webpack打包問題10. Python pyinstaller 打包后在其他電腦運行失敗
