2015-01-06 102 views
0

當我這樣做AJAX調用jQuery的AJAX請求總是失敗

var BASE_URL = "http://127.0.0.1:8000/index.php"; 
$.get(BASE_URL + "?r=category") 
      .done(function (data) { 
       alert('success'); 
      }) 
      .fail(function (error) { 
       alert('error'); 
      }); 

這是服務器響應(使用Fiddler檢查)

HTTP/1.1 200 OK 
Date: Tue, 06 Jan 2015 13:46:19 GMT 
Server: Apache/2.4.7 (Win32) PHP/5.5.8 
X-Powered-By: PHP/5.5.8 
X-Pagination-Total-Count: 3 
X-Pagination-Page-Count: 1 
X-Pagination-Current-Page: 1 
X-Pagination-Per-Page: 20 
Link: <http://127.0.0.1:8000/index.php?r=category%2Findex&page=1>; rel=self 
Content-Length: 163 
Keep-Alive: timeout=5, max=100 
Connection: Keep-Alive 
Content-Type: application/json; charset=UTF-8 

[{"CaID":1,"Name":"Brot und Gebäck","ParentCaID":null},{"CaID":2,"Name":"Süßes und Salziges","ParentCaID":null},{"CaID":3,"Name":"Getränke","ParentCaID":null}] 

但它總是會調用失敗的函數。

+1

你是否缺少從ajax.get傳遞page = 1? –

+0

嘗試$ .getJSON(),因爲你的內容類型是'application/json' –

+0

失敗回調中的錯誤是什麼?您的警報不是很有用的信息 – charlietfl

回答

0
var BASE_URL = "http://127.0.0.1:8000/index.php"; 
$.get(BASE_URL + "?r=category") 
     .done(function (data) { 
      alert('success'); 
     }) 
     .fail(function (error) { 
      console.log(error);//to show what error happended 
      alert('error'); 
     });