向外域发送JSONPOST请求,无需访问服务器

Send JSON POST request to external domain without having access to the server

我试图在 JSON 中向外部域发出 POST 请求,但我无法访问服务器的文件来修改它们。

当我执行此请求时,出现以下错误

XMLHttpRequest cannot load https://external.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.ownedwebsite.com' is therefore not allowed access.

问题出在哪里?

这是我使用的代码:

  $(document).ready(function(){
            $("#submit").on('click', function(){
                event.preventDefault();
                $.ajax({
                    url: 'https://external.com',
                    type : "POST",
                    crossDomain: true,
                    dataType : 'json',
                    beforeSend: function (request)
                    {
                    //request.setRequestHeader("name", "value");
                    },
                    data : $("#formCart").serialize(),
                    success : function(result) {
                    alert('POST done.');
                    },
                    error: function(xhr, resp, text) {
                        alert('POST failed.');
                    }
                })
            });
        });

我能做什么?我需要做的就是以 JSON 格式发送此 POST 表单数据。

如果您想从 twitter 获取 json 数据,您必须注册 api 密钥,这意味着它们已准备好与 json 一起使用,类似的方法适用于其他网站。

如果您只是想抓取页面,那么您可以使用 heroku api

$(document).ready(function(){
      var text = 'https://login.yahoo.com/';
      $.ajaxPrefilter( function (options) {
        if (options.crossDomain && jQuery.support.cors) {
          var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
          options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
          //options.url = "http://cors.corsproxy.io/url=" + options.url;
        }
      });

      $.get(
          'https://en.wikipedia.org/wiki/Thomas_Alva_Edison_Memorial_Tower_and_Museum',
          function (response) {

          var res = response;
           $('#yahoo').append(res);


      });

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="yahoo"></div>