jquery ajax satus為200卻出現進入error回呼函數

來源:互聯網
上載者:User
//請求載入檔案資料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(){***}   ” 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.