2013-03-22 13 views
0

嘗試接收從PHP這是一個不同的域到我的HTML的響應:連接到PHP在不同的域使用jQuery(AJAX)爲HTML

$.get('getstate.php', { 
       email: $('#game-email').val(), 
       country: 'DE', 
       lang: lang, 
       source: 'promotion' 
      }, function (data) { 
       console.log(data); 
      }); 

在Chrome中我得到這個錯誤:

XMLHttpRequest cannot load ..... Origin null is not allowed by Access-Control-Allow-Origin.

我需要做些什麼才能做到這一點?

+1

http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains – Eli 2013-03-22 11:00:54

+0

當然,如果你想獲得一個簡單的PHP代理'getstate.php'它應該是url的完整路徑,如果它位於另一個域 – chriz 2013-03-22 11:01:05

+0

最快的方法是通過本地php服務器和curl請求代理請求 – 2013-03-22 11:06:19

回答

0

嘗試數據類型: 'JSONP'
$獲得('page.php文件,{},回調 「JSONP」);

+0

服務器需要明顯支持jsonp – 2013-03-22 11:07:11

+0

有沒有其他方式沒有服務器必須支持jsonp? – user1937021 2013-03-22 12:33:31

+0

@ 1937021你有權訪問該服務器嗎? – 2013-03-22 13:01:27

0

http://jquery-howto.blogspot.co.uk/2009/04/cross-domain-ajax-querying-with-jquery.html

<?php 
// Set your return content type 
header('Content-type: application/xml'); 

// Website url to open 
$daurl = 'http://feeds.feedburner.com/jQueryHowto'; 

// Get that website's content 
$handle = fopen($daurl, "r"); 

// If there is something, read and return 
if ($handle) { 
    while (!feof($handle)) { 
     $buffer = fgets($handle, 4096); 
     echo $buffer; 
    } 
    fclose($handle); 
} 
?>