2012-11-14 52 views
0

我搜索了這個,但大部分與此相關的問題都是針對其他服務的API。GET Json API結果

我正在構建一個API,允許遊戲開發者從我的數據庫發送和檢索用戶信息。

我終於可以放在一起的API,但現在我需要調用API。

當遊戲啓動時,它會向遊戲開發者發送關鍵的開發人員ID和遊戲ID。

//Game loads, get developer key, send token and current high score 

// == [ FIRST FILTER - FILTER GET REQUEST ] == // 
$_GET = array_map('_INPUT', $_GET); // filter all input 


// ====================================== // 
// ============[ ACTION MENU ]=========== // 
// ====================================== // 

if(!empty($_GET['action']) && !empty($_GET['user']) && !empty($_GET['key']) && !empty($_GET['email']) && !empty($_GET['password'])): // if key data exists 

switch($_GET['action']): 

//athenticate game developer return and high score 
case 'authenticate': 

    $db = new PDO('mysql:host=localhost;dbname=xxxx', 'xxxx', 'xxxx'); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

    $st = $db->prepare("SELECT * FROM `game_developers_games` WHERE `id` = :gameid AND `developer_id`=:user AND `key`= :key AND `developer_active` = '1'"); // need to filter for next auction 
    $st->bindParam(':user', $_GET['user']); // filter 
    $st->bindParam(':key', $_GET['key']); // filter 
    $st->execute(); 
    $r = $st->fetch(PDO::FETCH_ASSOC); 

    if($st->rowCount() == 0): 

     $return = array('DBA_id'=>'0000'); 
     echo json_encode($return); 

    else: 

     $token = initToken($_GET['key'],$_GET['user']); 

     if($token == $r['API_Token']): 

      $return = array(
      'DBA_id'=>$token, 
      'DBA_servertime'=>time(), 
      'DBA_highscore'=>$r['score'], 
      ); 

      echo json_encode($return);     

     endif; 

    endif; 

    break; 

這裏的腳本的遊戲開發商將不得不加入到他們的遊戲中獲得的數據進行遊戲加載時。發現這在另一個stackoverflow問題,但它不工作。

$.getJSON("https://www.gamerholic.com/gamerholic_api/db_api_v1.php? user=1&key=6054abe3517a4da6db255e7fa27f4ba001083311&gameid=1&action=authenticate",   function() { 
     alert("aaa"); 

       }); 
+0

您目前正在返回JSON,是'https://開頭www.gamerholic.com'即發出請求的同一個網站? –

+0

http://enable-cors.org/ –

回答

0

嘗試增加&回調=?到您正在構建的網址的末尾。這將啓用被cors接受的jsonp。

$.getJSON("https://www.gamerholic.com/gamerholic_api/db_api_v1.php?user=1&key=6054abe3517a4da6db255e7fa27f4ba001083311&gameid=1&action=authenticate&callback=?",   function() { 
    alert("aaa"); 
}); 
+0

添加回調不起作用 – user1745606

+0

您是否嘗試過使用Chrome或Firebug進行調試? –

0

根據跨域來源策略,您無法使用jquery getJson函數訪問跨域url。

使用json管理跨域請求需要回調,並且需要在服務器和客戶端上處理。

還要確保使用螢火蟲或類似工具,因爲以現在它返回的響應代碼爲200。

我在這裏提兩個線程,可以指導正確的方式

Jquery getJSON cross domain problems檢查響應

http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/