2015-07-21 118 views
2

我正在構建這個HTML/JS應用程序,以便基於我的WordPress網站(使用其數據庫,用戶等)基於Phonegap進行移動。 所以現在我有一個functions.js試圖訪問我的wordpress創建了一個功能類似在外部主機上訪問Wordpress ajax

add_action('wp_ajax_nopriv_checking_email', 'checking_email'); 

我要的是能夠與我的JS代碼訪問此一個index.html。 我已經試過這樣的事情:

jQuery.ajax({ 
    type: 'POST', 
    url: 'http://myhost.com/wp-admin/admin-ajax.php', 
    data: { 'action': 'checking_email', 'email': '[email protected]' }, 
    success: function(request_data) { 
     console.log(request_data); 
    } 
}); 

,我得到

XMLHttpRequest cannot load http://myhost.com/wp-admin/admin-ajax.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我能做些什麼,以從外部主機訪問管理-ajax.php?

+0

您是否需要從不同的域名?如果是這樣,這是一個CORS問題。請閱讀這篇文章以獲取更多信息http://www.html5rocks.com/en/tutorials/cors/基本上,您的主機必須遵循允許不同域訪問其資源的規則。 –

+0

@VictorSoto這不是關於主機......它只涉及服務器腳本中的標題 – charlietfl

+0

@VictorSoto感謝您的信息,但正如charlietfl所說的,我只需要更改PHP腳本的訪問控制。 – Dogmatics

回答

1

因此,經過多次測試後,我發現它確實是一個非常愚蠢的修復。 通過更改服務器腳本的標題,它允許從不同的主機訪問它。

header('Access-Control-Allow-Origin: *'); // for any host 
header('Access-Control-Allow-Origin: myhost.com'); // specific host