也許你可以更好地理解它,那麼我可以。我很擅長php,但不擅長髮送不同的標題和內容。它的工作原理是將一個ajax帖子發佈到代理文件中。代理文件位於之外服務器上
$.ajax({
type: "POST",
url: "http://mywebsite.com/ajax-proxy.php",
data: 'csurl=www.google.com',
error: function(e) {console.log(e);},
success: function(msg){console.log(msg)}
});
你也得通過csurl這是URL,代理你轉發給。在這個例子中我使用谷歌。但我會爲csurl正常使用是目錄的地方,我將存儲AJAX數據
在代理文件中,有一個
$valid_requests = array()
在該數組,你的國家,你想要的所有網址代理商要批准。在這個例子中,你把www.google.com(注意:必須是完全一樣csurl參數,或者它不會工作)
下面是代理文件
<?php
/**
* AJAX Cross Domain (PHP) Proxy 0.6
* by Iacovos Constantinou (http://www.iacons.net)
*
* Released under CC-GNU GPL
*/
/**
* Enables or disables filtering for cross domain requests.
* Recommended value: true, for security reasons
*/
define('CSAJAX_FILTERS', true);
/**
* A set of valid cross domain requests
*/
$valid_requests = array(
'www.google.com'
);
/*** STOP EDITING HERE UNLESS YOU KNOW WHAT YOU ARE DOING ***/
// identify request headers
$request_headers = array();
foreach ($_SERVER as $key=>$value) {
if(substr($key, 0, 5) == 'HTTP_') {
$headername = str_replace('_', ' ', substr($key, 5));
$headername = str_replace(' ', '-', ucwords(strtolower($headername)));
$request_headers[$headername] = $value;
}
}
// identify request method, url and params
$request_method = $_SERVER['REQUEST_METHOD'];
$request_params = ($request_method == 'GET') ? $_GET : $_POST;
$request_url = urldecode($request_params['csurl']);
$p_request_url = parse_url($request_url);
unset($request_params['csurl']);
// ignore requests for proxy :)
if (preg_match('!'. $_SERVER['SCRIPT_NAME'] .'!', $request_url) || empty($request_url)) {
exit;
}
// check against valid requests
if (CSAJAX_FILTERS) {
$parsed = $p_request_url;
$check_url = isset($parsed['scheme']) ? $parsed['scheme'] .'://' : '';
$check_url .= isset($parsed['user']) ? $parsed['user'] . ($parsed['pass'] ? ':'. $parsed['pass']:'') .'@' : '';
$check_url .= isset($parsed['host']) ? $parsed['host'] : '';
$check_url .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
$check_url .= isset($parsed['path']) ? $parsed['path'] : '';
if (!in_array($check_url, $valid_requests)) {
exit;
}
}
// append query string for GET requests
if ($request_method == 'GET' && count($request_params) > 0 && (!array_key_exists('query', $p_request_url) || empty($p_request_url['query']))) {
$request_url .= '?'. http_build_query($request_params);
}
// let the request begin
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); // (re-)send headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return response
curl_setopt($ch, CURLOPT_HEADER, true); // enabled response headers
// add post data for POST requests
if ($request_method == 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request_params));
}
// retrieve response (headers and content)
$response = curl_exec($ch);
curl_close($ch);
// split response to header and content
list($response_headers, $response_content) = preg_split('/(\r\n){2}/', $response, 2);
// (re-)send the headers
$response_headers = preg_split('/(\r\n){1}/', $response_headers);
foreach ($response_headers as $key => $response_header)
if (!preg_match('/^(Transfer-Encoding):/', $response_header))
header($response_header);
// finally, output the content
print($response_content);
?>
同樣,如果我把http://mywebsite.com/ajax-proxy.php?csurl=www.google.com從在我的網站內。它工作正常。甚至把它放在網址中。工作正常。但如果你使用ajax post從外部服務器調用它。不工作。
我聽說過iframe,但我不知道從哪裏開始。我認爲這會幫助我很多。不知道它是否會起作用。但它似乎是一個好開始。 thanx很多 – numerical25 2009-11-06 15:51:09
如果你知道任何其他偉大的例子,也許是一本書或URL你可以引薦我,請做和thanx – numerical25 2009-11-06 15:57:41
有大量的點擊,如果你谷歌「使用隱藏的形式發佈數據跨域」,例如。 http://w-shadow.com/blog/2008/11/20/cross-domain-post-with-javascript/,但技術非常簡單。缺點是你無法從帖子中取回任何數據,所以它只是一個單向流。 – Cal 2009-11-07 21:54:36