我已经看了两天教程了,我真的不明白我的选择是什么。我有一台带HMI的机器,运行网络服务器(我不知道那是什么类型的网络服务器)。我可以通过json数据访问POST请求中的HMI标签值。rquest的例子看起来像这个
$( document ).ready(function() {
var data0 = {"getTags":["Start_dav","CutON","run_rot_actual"],"includeTagMetadata":true};
var json = JSON.stringify(data0 );
$.ajax({
url: "http://ipaddress/tagbatch",
type: "POST",
dataType: "json",
data: json,
响应是json数据。
当然,问题出在跨领域政策上。我无法控制机器,也无法设置CORS。我读过关于proxy、iframe和yql解决方案的文章,但据我所知,我无法使用这些解决方案发送json数据。有没有办法用json跨域发送post请求?
感谢您对的帮助
我找到了一个解决方案。我正在使用php-ccurl发送请求。
这是工作代码:
$data = array("getTags" => array("Var1","Var2"), "includeTagMetadata" => false);
$data_string = json_encode($data);
$agent= 'Mozilla/5.0 (Windows; U;Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9';
//open connection
$ch = curl_init();
$f = fopen('request.txt', 'w'); //writes headers to this file
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,"domainipaddress");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
$tmpfname = dirname(__FILE__).'/cookie.txt'; //saves the cookie from server
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
curl_setopt( $ch, CURLOPT_STDERR, $f );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_FORBID_REUSE, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Connection: keep-alive',
"Keep-Alive: 300"
)
);
//execute post
$result = curl_exec($ch);
$headers = curl_getinfo($ch);
fclose($f);