Cross ajax domain by using proxy

2019-06-07 14:09发布

I'm currently trying to make some ajax post between cross-domains by following this tutorial but something wrong some data wasn't send.

Actually my proxy script is a copy of the tutorial and this my 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条回答
Animai°情兽
2楼-- · 2019-06-07 14:52

Solved by making a php script with curl :

//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);
查看更多
登录 后发表回答