admin管理员组文章数量:1399979
How do you post JSON data as a url string to an external url (cross domains) and bypass Access Control?
Here is a jquery .ajax post request that won't work sending to an 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 a suggestion to POST the data to the same domain and 'pass on the request' to the external domain, though this solution doesn't make sense to me. I am looking for the most secure solution. Any help would be much appreciated.
How do you post JSON data as a url string to an external url (cross domains) and bypass Access Control?
Here is a jquery .ajax post request that won't work sending to an 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 a suggestion to POST the data to the same domain and 'pass on the request' to the external domain, though this solution doesn't make sense to me. I am looking for the most secure solution. Any help would be much appreciated.
Share Improve this question asked Apr 5, 2013 at 0:31 Nicholas.VNicholas.V 1,9362 gold badges16 silver badges30 bronze badges 7- Have you tried using JSON-P? I believe in jQuery you use 'jsonp' instead of 'json' for the data type to do so, but I'm not 100% certain... – Mara Ormston Commented Apr 5, 2013 at 0:34
- 1 When you say the 'pass on therequest' solution doesn't make sense to you do you mean simply that you don't understand it and would like an explanation, or that you do understand it but think it isn't appropriate for your current situation? @MarkOrmston - JSONP does let you work around the domain issue, but it will only work if the external domain is setup to handle it and provide an appropriate resposne. – nnnnnn Commented Apr 5, 2013 at 0:37
- Yeah, it is not suitable in this case, the data has to be sent as json. I also do not have any control over the external server, thus why CORS also isn't a possible solution. – Nicholas.V Commented Apr 5, 2013 at 0:39
- @jverban if you do not have control over the other domain service then possibly it is not possible to get a response from that. Probably check this out..stackoverflow./questions/15534640/… – PSL Commented Apr 5, 2013 at 0:39
- OK, I don't understand why you think the data being sent as JSON prevents you from doing the 'pass the request' thing. Your jQuery would do an Ajax request to your own PHP, and your PHP would call the other server and then take its response and return it to the browser. This is do-able with or without JSON... – nnnnnn Commented Apr 5, 2013 at 0:42
2 Answers
Reset to default 3I did this not too long ago in PHP. Here's an example of "passing the request". (You'll need to enable PHP cURL, which is pretty standard with most installations.)
<?php
//Get the JSON data POSTed to the page
$request = file_get_contents('php://input');
//Send the JSON data to the right server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://location_of_server./");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$data = curl_exec($ch);
curl_close($ch);
//Send the response back to the Javascript code
echo $data;
?>
One way to bypass the Same-Origin policy is to use cURL to do the actual transmitting.
I'll give an example using PHP, but you could easily do this on any server side language.
Set up a script on your server, for example send.php
First you point your ajax to 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 your php script to 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);
// Get the results
$result = curl_exec($curl);
// Close resource
curl_close($curl);
echo $result;
?>
本文标签: phpPost JSON data to external URLStack Overflow
版权声明:本文标题:php - Post JSON data to external URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744195672a2594730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论