2011-12-21 98 views
1

我目前正試圖通過以下this tutorial跨域之間做一些AJAX帖子,但有些錯誤的一些數據沒有發送。通過使用代理跨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

通過使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);