2016-03-31 23 views
0

私は2日間チュートリアルを読んでいます。私は本当に私の選択肢が何であるか理解できません。私はWebサーバーを実行するHMIを持つマシンを持っています(私はどんな種類のWebサーバーであるかわかりません)。私は、HMIタグに、jsonデータでのPOSTリクエストを行います。 rquestの例では、応答はJSONデータであり、このjson data cross domainでの投稿のリクエスト

$(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, 

のように見えます。

問題はもちろんクロスドメインポリシーです。私はマシンを制御することができず、CORSを設定するオプションはありません。私はプロキシ、iframeとyqlのソリューションについては読んだが、私は理解できないので、これらの回避策でjsonデータを送信することはできない。 jsonのクロスドメインでポストリクエストを送信する方法はありますか?

ありがとうございました

答えて

0

解決策が見つかりました。私はPHP curlを使ってリクエストを送信しています。ここで

は、作業コードです:

$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); 
関連する問題