2011-05-03 59 views
1

我正在關注Sencha Touch的這個教程:http://www.vimeo.com/15672696 完美的教程,工作完美無瑕。JSONP與Sencha Touch不起作用

但是,當我試圖重複使用這個代碼爲我自己的項目,我不工作。 這是笏我做:

在我的煎茶觸摸應用程序,我寫了下面的功能:

showContacts = function() 
{ 
    Ext.util.JSONP.request({ 
     url: 'http://www.hotcoffee.be/check-relations/index.php/json/contactpersonen', 
     callbackKey: "callback", 
     params: { 
      unique: Math.random() 
     }, 
     callback: function(data) 
     { 
      var contacts = data.results; 
      nameOfPanel.update(contacts); 
     } 
    }); 
} 

首先,我寫我自己的JSON文件,PHP(笨):

<? 
    $row[] = array(
     "name" => $item->name, 
     "first_name" => $item->first_name, 
     "avatar"=>$item->avatar 
    ); 

    // PASSING THE ARRAY $row TO A VIEW 
    // ON THE VIEW I OUTPUT THE ARRAY 

    $this->output->set_content_type('application/json')->set_output(json_encode($row)); 
?> 

(結果:請參閱showContacts函數的URL)

這沒有奏效,所以我認爲使用PHP文件作爲JSON時出了問題,所以我創建了一個JSON文件:

(結果:http://www.hotcoffee.be/check-relations/json/friends.json

現在看來,這也沒有工作。我打破我的頭過了很多houres的...... 我也是在Chrome的調試器收到以下消息:

Resource interpreted as Script but transferred with MIME type application/json. (contactpersonen:-1) 
Uncaught SyntaxError: Unexpected token : (contactpersonen:1) 
Resource interpreted as Image but transferred with MIME type text/html. (csi:-1) 

另一件事是,我可以保證面板和TPL的都寫不好,因爲與硬編碼測試數據它工作。只加載JSON文件是個問題。

任何人都知道如何處理它? 非常感謝!

+0

JSONP通過在回調函數中包裝JSON來工作(以便它可以包含在',這隻適用於我 - 即不要使用上面的第1行和第3行 – pepe 2012-09-16 15:38:51

3

這是PHP服務器的例子:

$callback = $_REQUEST['callback']; 

// Create the output object. 
$output = array('a' => 'Apple', 'b' => 'Banana'); 

//start output 
if ($callback) { 
    header('Content-Type: text/javascript'); 
    echo $callback . '(' . json_encode($output) . ');'; 
} else { 
    header('Content-Type: application/x-json'); 
    echo json_encode($output); 
} 

當你在Chrome或Firefox調試它,你可以發現反應是與Ext.util.JSONP.callback({})

加入該是多麼JSONP「從與運行頁面的原始域不相同的域中的頁面檢索數據「,並將數據提供給您正在運行的頁面。