前端页面Ajax 请求,dataType 未区分大小写,导致返回的数据类型异常
在一次开发过程中,前端页面用到了Ajax请求,且均指定了json。但最终就是获取不到后台返回来的数据。经过排查,原来是dataType
写成了datatype 。一起来看下代码先:
$.ajax({ url:"/trustWeb/getAjaxTips.do?method=getFundAcco", datatype : "json", type : "POST", async : false, data : {"custType":custtype,"identityType":identitytype,"identityNo":encodeURIComponent(identityno),"custName":encodeURIComponent(custname)}, success : function(data){ fundAcco= data.refundAcco; } });
使得请求成功后,返回的data数据为:{"refundAcco":"WX8099912"}, fundAcco=data.refundAcco的形式获取不到数据,fundAcco显示为undefind。
然而将datatype改成dataType,返回的data数据为[Object,Object], fundAcco=data.refundAcco的形式成功获取到数据,fundAcco显示为WX8099912。
哎~,有时候真是细节决定成败呀!!