通过使用代理十字AJAX领域 [英] Cross ajax domain by using proxy

查看:90
本文介绍了通过使用代理十字AJAX领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

其实我的代理脚本是教程的副本,这是我的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);
 

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

这篇关于通过使用代理十字AJAX领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆