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
 |  Show 2 more ments

2 Answers 2

Reset to default 3

I 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