简体   繁体   English

将JSON数据发布到外部URL

[英]Post JSON data to external URL

How do you post JSON data as a url string to an external url (cross domains) and bypass Access Control? 如何将JSON数据作为URL字符串发布到外部URL(跨域)并绕过访问控制?

Here is a jquery .ajax post request that won't work sending to an external url because of Access-Control-Allow-Origin: 这是一个jquery .ajax发布请求,由于Access-Control-Allow-Origin而无法发送到外部URL:

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. 任何帮助将非常感激。

I did this not too long ago in PHP. 我不久前在PHP中完成了此操作。 Here's an example of "passing the request". 这是“传递请求”的示例。 (You'll need to enable PHP cURL, which is pretty standard with most installations.) (您需要启用PHP cURL,这在大多数安装中都是非常标准的。)

<?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.com/");
    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. 绕过Same-Origin策略的一种方法是使用cURL进行实际传输。

I'll give an example using PHP, but you could easily do this on any server side language. 我将举一个使用PHP的示例,但是您可以轻松地在任何服务器端语言上执行此操作。

Set up a script on your server, for example send.php 在服务器上设置脚本,例如send.php

First you point your ajax to send.php 首先,您将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 your php script to forward it: 然后,您的php脚本将其转发:

<?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;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM