您的位置:首页 > Web前端 > JQuery

jquery ajax satus为200却出现进入error回调函数

2015-06-03 17:17 731 查看
//请求载入文件数据
var loadFile=function(){
$.ajax({
url:'filePreView.do',
data:{'id':id,'md5':md5},
type:'post',
dataType:'json',
cache:false,
success:function (data){
switch (data.statusCode){
case 0:
case 1:
case 2:
case 3:
alert('3');
reloadFile();
break;
case 4:
alert('4');
showFile(data);
break;
case -1:
alert('-1');
showFailed();
break;
default :
showFailed();
}
},
error:function(data){
alert('error');
后台返回:
<ol tabindex="0" class="outline-disclosure" style="font: 12px/normal 'Segoe UI', Tahoma, sans-serif; margin: 0px; color: rgb(34, 34, 34); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; list-style-type: none; white-space: normal; box-sizing: border-box; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-padding-start: 12px; -webkit-text-stroke-width: 0px;"><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">Remote Address:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">[::1]:8080</div></li><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">Request URL:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">http://localhost:8080/file_preview/filePreView.do</div></li><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">Request Method:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">POST</div></li><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">Status Code:</div><div title="200 OK" class="resource-status-image green-ball" style="background-position: -235px -96px; width: 10px; height: 10px; margin-top: -2px; margin-right: 3px; vertical-align: middle; display: inline-block; box-sizing: border-box; background-image: url('chrome-devtools://devtools/bundled/Images/statusbarButtonGlyphs.png'); background-size: 320px 144px;"></div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">200 OK</div></li><li title="" class="parent expanded" style="padding: 0px 0px 0px 14px; font-weight: bold; margin-top: 1px; margin-left: -12px; -ms-word-wrap: break-word; box-sizing: border-box; -webkit-user-select: none;">Request Headers<span class="header-toggle" style="color:#737373;font-weight: normal; margin-left: 30px; display: inline; box-sizing: border-box;">view parsed</span></li><ol class="children expanded" style="margin: 0px; padding: 2px 6px !important; display: block; list-style-type: none; position: relative; cursor: default; min-width: 100%; box-sizing: border-box; -webkit-padding-start: 12px;"><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; white-space: nowrap; -ms-word-wrap: break-word; box-sizing: border-box;"><span class="header-value source-code" style="font-family:Consolas, 'Lucida Console', monospace;font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">POST /file_preview/filePreView.do HTTP/1.1Host: localhost:8080Connection: keep-aliveContent-Length: 72Accept: application/json, text/javascript, */*; q=0.01Origin: http://localhost:8080X-Requested-With: XMLHttpRequestUser-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 LBBROWSERContent-Type: application/x-www-form-urlencodedReferer: http://localhost:8080/file_preview/apply.doAccept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.8Cookie: JSESSIONID=84BBEE2A60F765B810CFFBEC145DE240</span></li></ol><li title="" class="parent expanded" style="padding: 0px 0px 0px 14px; font-weight: bold; margin-top: 1px; margin-left: -12px; -ms-word-wrap: break-word; box-sizing: border-box; -webkit-user-select: none;">Form Data<span class="header-toggle" style="color:#737373;font-weight: normal; margin-left: 30px; display: inline; box-sizing: border-box;">view source</span><span class="header-toggle" style="color:#737373;font-weight: normal; margin-left: 30px; display: inline; box-sizing: border-box;">view URL encoded</span></li><ol class="children expanded" style="margin: 0px; padding: 2px 6px !important; display: block; list-style-type: none; position: relative; cursor: default; min-width: 100%; box-sizing: border-box; -webkit-padding-start: 12px;"><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; white-space: nowrap; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">id:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">b6de6638253943a19890ac4f9dd2587c</div></li><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; white-space: nowrap; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">md5:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">de2f59c0cec5ce16ae84b74d7a3af046</div></li></ol><li title="" class="parent expanded" style="padding: 0px 0px 0px 14px; font-weight: bold; margin-top: 1px; margin-left: -12px; -ms-word-wrap: break-word; box-sizing: border-box; -webkit-user-select: none;">Response Headers<span class="header-toggle" style="color:#737373;font-weight: normal; margin-left: 30px; display: inline; box-sizing: border-box;">view source</span></li><ol class="children expanded" style="margin: 0px; padding: 2px 6px !important; display: block; list-style-type: none; position: relative; cursor: default; min-width: 100%; box-sizing: border-box; -webkit-padding-start: 12px;"><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; white-space: nowrap; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">Content-Length:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">675</div></li><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; white-space: nowrap; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">Content-Type:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">application/json;charset=UTF-8</div></li><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; white-space: nowrap; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">Date:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">Wed, 03 Jun 2015 09:11:02 GMT</div></li><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; white-space: nowrap; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-name" style="color: rgb(84, 84, 84); font-weight: bold; margin-right: 0.5em; vertical-align: top; display: inline-block; white-space: pre-wrap; box-sizing: border-box;">Server:</div><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;">Apache-Coyote/1.1</div></li></ol><li title="" style="padding: 0px 0px 0px 14px; margin-top: 1px; margin-left: -2px; white-space: nowrap; -ms-word-wrap: break-word; box-sizing: border-box;"><div class="header-value source-code" style="font-family: Consolas, 'Lucida Console', monospace; font-size: 12px !important; margin-top: 1px; margin-right: 1em; display: inline; white-space: pre-wrap; -ms-word-break: break-all; box-sizing: border-box;"> </div></li></ol><ol tabindex="0" class="outline-disclosure" style="font: 12px/normal 'Segoe UI', Tahoma, sans-serif; margin: 0px; color: rgb(34, 34, 34); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; list-style-type: none; white-space: normal; box-sizing: border-box; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-padding-start: 12px; -webkit-text-stroke-width: 0px;">状态200;但是确alert了error;</ol>


解决方式:

1. 返回的数据类型一定要符合定义的数据类型。即如果你定义的 dataType 是 json 类型的,那么返回来的数据一定是 json 才可以,平且不然就会执行 error 里的程序块儿。

(1) 同时需要特别的注意返回的JSON数据是否是严格的JSON格式.

(2) 也应该严重关切当后台返回的是一个List 数据(List当中的数据是Json格式)时,有没脏数据即不是严格的JSON格式。

很隐蔽的可能是数据某一个字段中在开始或末尾含有特殊字符,以"回车键"、"Tab键"等

这类隐蔽错误的规避措施就是好的编程习惯:

a. 对于在页面填写入库的数据最好强制性的做,去空操作<利用客户端最好>。

b. 特殊的业务需注意特殊字符。

这样还不够,因为在开发过程中测试或开发人员自己在数据库中手动添加数据,可能会多加了空格等,导致程序调试,测试带来了麻烦。 这时就需要考虑在后台代码对获取的数据做处理。

2. 原因是ajax请求跨域了,解决方法是在两个文件里都添加一段 js: [/b]document.domain,或者采用Jsonp的方式,如我的前一篇blog中提到的。

还有一点对JQuery 中Ajax的一点其它的认识: 客户端发起请求,得到服务器端的相应是200,<正确拿到服务器响应的数据>没有问题.此时在判断进入success 对应的回调函数还是进入到error对应的回调函数之前,可能会校验一些东西:

1. 返回的每条数据是否是dataType中定义的数据类型。如果有部分数据不是或者哪怕一条数据没有严格的按照dataType定义的类型,程序就会进入到error:function(){****}

2. 请求的域和当前域是否是同一域,如果不是同一域也十分有可能进入error:function(){***} ”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: