2013-10-11 49 views
0

我得到一個錯誤,在我的請求時,我嘗試從我的PHP API使用信息:JSON:錯誤域

OPTIONS http://api.reddrummer.com/summary.json?api_key=tok 405 (Method Not Allowed) jquery.js:130 
OPTIONS http://api.reddrummer.com/summary.json?api_key=tok Invalid HTTP status code 405 jquery.js:130 
XMLHttpRequest cannot load http://api.reddrummer.com/summary.json?api_key=tok. Invalid HTTP status code 405 

(TOK是我的令牌訪問來自API的信息)

(function($){ 
    document.addEventListener("deviceready", function(){ 
     tok = window.sessionStorage.getItem("token"); 
     jQuery.ajax({ 
      type: 'GET', 
      url: 'http://api.reddrummer.com/summary.json?api_key=' + tok, 
      contentType: "application/json", 
      dataType: 'json', 
      success: function(d) { 
       alert("ok"); 
      }, 
      error: function(error){ 
       alert("not ok"); 
      } 
     }); 
    }, false); 
})(jQuery); 

這是我的AJAX技術,所以我改變了數據類型來接受,而不是JSONP JSON的:

(function($){ 
    document.addEventListener("deviceready", function(){ 
     tok = window.sessionStorage.getItem("token"); 
     jQuery.ajax({ 
      type: 'GET', 
      url: 'http://api.reddrummer.com/summary.json?api_key=' + tok, 
      contentType: "application/json", 
      dataType: 'jsonp', 
      jsonpCallback: 'callback', 
      jsonp: false, 
      success: function(d) { 
       alert("ok"); 
      }, 
      error: function(error){ 
       alert("not ok"); 
      } 
     }); 
    }, false); 
})(jQuery); 

現在還出現了另一個錯誤:

Uncaught SyntaxError: Unexpected token : summary.json:2 

它在我的JSON的錯誤,所以我已經驗證了我的JSON,有什麼不對的吧:

{ 
    "username": "Guilherme", 
    "language": "pt-br", 
    "position": "Java Pleno", 
    "company_name": "Reddrummer", 
    "photo": "http://drumcircle.reddrummer.com/p/photo/user/[email protected]", 
    "post": { 
     "total_post": "1", 
     "total_comments": "1", 
     "groups": 1, 
     "groups_url": { 
      ".Delivery Brasil": "http://drumcircle.reddrummer.com/#houseorgan/2566054" 
     }, 
     "people": 2 
    }, 
    "chart": { 
     "_video": 0, 
     "_image": 0, 
     "project": 0, 
     "topic": 0, 
     "note": 0, 
     "mobile": 0, 
     "task": 0, 
     "press": 0, 
     "bug": 0, 
     "reuniao": 1, 
     "sap": 0, 
     "oracle": 0, 
     "sharep": 0, 
     "vendas": 0, 
     "ideia": 0 
    } 
} 

我需要一些幫助,因爲我想看到的文檔和許多其他網站,但錯誤總是相同的,當我試圖從我的API與JSONP接收值。

+1

一個JSONP反應是不一樣的JSON響應。你得到JSON,所以這應該是你的'dataType'。對我來說,你有一個服務器問題。 – user2736012

回答

0

JSONP可以從另一個域請求服務/數據。格式與經典的JSON對象略有不同。

您的服務需要將數據包裝到回調函數中並將其作爲參數傳遞。這個回調通常是動態的,並作爲查詢字符串參數傳遞(即jQuery通過追加'callback'參數鍵自動執行它)。

試想URL的jQuery $就稱爲()是http://api.reddrummer.com/summary.json?api_key=12345&callback=jQuery152031713143712840974_1378285818229&_=1378285824291,服務需要返回:

jQuery152031713143712840974_1378285818229(
{ 
    "username": "Guilherme", 
    "language": "pt-br", 
    "position": "Java Pleno", 
    "company_name": "Reddrummer", 
    "photo": "http://drumcircle.reddrummer.com/p/photo/user/[email protected]", 
    "post": { 
     "total_post": "1", 
     "total_comments": "1", 
     "groups": 1, 
     "groups_url": { 
      ".Delivery Brasil": "http://drumcircle.reddrummer.com/#houseorgan/2566054" 
     }, 
     "people": 2 
    }, 
    "chart": { 
     "_video": 0, 
     "_image": 0, 
     "project": 0, 
     "topic": 0, 
     "note": 0, 
     "mobile": 0, 
     "task": 0, 
     "press": 0, 
     "bug": 0, 
     "reuniao": 1, 
     "sap": 0, 
     "oracle": 0, 
     "sharep": 0, 
     "vendas": 0, 
     "ideia": 0 
    } 
} 
);