您的位置:首頁技術文章
文章詳情頁

java - ajax成功到后臺不知道為什么一直回調失敗函數

瀏覽:118日期:2023-12-03 17:34:06

問題描述

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

標簽: java
相關文章:
国产综合久久一区二区三区