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:請(qǐng)求成功有數(shù)據(jù)返回,很大可能與你的返回?cái)?shù)據(jù)格式不對(duì)有關(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ù)的,請(qǐng)自行打印出來。
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請(qǐng)求和response不一樣,得到數(shù)據(jù)后頁面不需要再渲染,所以不需要RESPONSE跳轉(zhuǎn)到新頁面。所以不需要RETURN,而是通過PrintWriter打印到請(qǐng)求的頁面@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是跨域請(qǐng)求的 。
回答7:你的返回值數(shù)據(jù)類型是json,你后臺(tái)卻給他返回了一個(gè)Map,把你的map轉(zhuǎn)成json
相關(guān)文章:
1. mysql時(shí)間格式問題2. 數(shù)組排序,并把排序后的值存入到新數(shù)組中3. 默認(rèn)輸出類型為json,如何輸出html4. mysql - msyql 判斷字段不為空 簡單方法5. mysql的主從復(fù)制、讀寫分離,關(guān)于從的問題6. mysql - sql 左連接結(jié)果union右連接結(jié)果,導(dǎo)致重復(fù)性計(jì)算怎么解決?7. MySQL的聯(lián)合查詢[union]有什么實(shí)際的用處8. php多任務(wù)倒計(jì)時(shí)求助9. mysql 遠(yuǎn)程連接出錯(cuò)10060,我已經(jīng)設(shè)置了任意主機(jī)了。。。10. PHP訂單派單系統(tǒng)
