php - Post JSON data to external URL -


how post json data url string external url (cross domains) , bypass access control?

here jquery .ajax post request won't work sending external url because of access-control-allow-origin:

var json = json.stringify(object);  $.ajax({   type: 'post',   url: externalurl,   data: json,   datatype: 'json',   success: function(data){console.log(data);},   failure: function(errmsg) {       console.log(errmsg);   }, }); 

i have received suggestion post data same domain , 'pass on request' external domain, though solution doesn't make sense me. looking secure solution. appreciated.

one way bypass same-origin policy use curl actual transmitting.

i'll give example using php, on server side language.

set script on server, example send.php

first point ajax send.php

var json = json.stringify(object);  $.ajax({     type: 'post',     url: send.php,     data: json,     datatype: 'json',     success: function(data){console.log(data);},     failure: function(errmsg) {         console.log(errmsg);     }, }); 

then php script forward it:

<?php     // initialize curl     $curl = curl_init();      // configure curl options     $opts = array(         curlopt_url             => $externalscriptaddress,         curlopt_returntransfer  => true,         curlopt_customrequest   => 'post',         curlopt_post            => 1,         curlopt_postfields      => 'field1=arg1&field2=arg2'     );      // set curl options     curl_setopt_array($curl, $opts);      // results     $result = curl_exec($curl);      // close resource     curl_close($curl);      echo $result; ?> 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -