通过使用代理十字AJAX领域领域、AJAX

2023-09-11 01:14:05 作者:私有,N1的爱

我目前正试图通过下面的本教程但蹊跷的一些数据没有被发送。

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

  $。阿贾克斯({
    键入:POST,
数据:数据+'和;起源='+产地,
网址:customer.php,
数据类型:JSON,
异步:假的,
成功:函数(结果){
    如果(result.id&安培;&安培; result.quotation_id){
        ID = result.id;
        quotation_id = result.quotation_id;
    }
    }
});
 

解决方案

通过PHP脚本,卷曲解决:

  //设置POST变量
$ URL ='http://my-different-domain.com';

$领域=阵列();

的foreach($ _ POST为$关键=> $值){
    $领域[$关键] = urlen code($值);
}

// URL-IFY的数据为POST
的foreach($领域$关键=> $值){$ fields_string = $键'='$值'和;'。; }
RTRIM($ fields_string,'&安培;');

//打开连接
$ CH = curl_init();

//设置URL,POST数瓦尔,POST数据
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_POST,计数($域));
curl_setopt($ CH,CURLOPT_POSTFIELDS,$ fields_string);

//实行岗位
$结果= curl_exec($ CH);

//关闭连接
curl_close($ CH);
 

Hybrid App开发设计与实现

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;
    }
    }
});

解决方案

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);