使用代理交叉ajax域

时间:2011-12-21 02:00:33

标签: ajax cross-domain

我目前正试图通过关注this tutorial在跨域之间发布一些ajax帖子,但是有些错误的数据未发送。

实际上我的代理脚本是教程的副本,这是我的javascript:

$.ajax({
    type: 'POST',
data: data + '&origin=' + origin, 
url: 'customer.php', 
dataType: 'json',
async: false,
success: function(result){
    if (result.id && result.quotation_id){
        id = result.id;
        quotation_id = result.quotation_id;
    }
    }
});

1 个答案:

答案 0 :(得分:1)

通过使用curl创建一个php脚本来解决:

//set POST variables
$url = 'http://my-different-domain.com';

$fields = array();

foreach ($_POST as $key => $value) {
    $fields[$key] = urlencode($value);
}

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);