2016-11-22 80 views
-1

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

当我做到这一点要求,我得到以下错误

的XMLHttpRequest无法加载https://external.com。否 “访问控制 - 允许来源”标题出现在请求的 资源中。原因'https://www.ownedwebsite.com'因此不允许 允许访问。

问题在哪里?

下面是我使用的代码:

$(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表单数据。

+0

[jQuery的阿贾克斯()POST请求的可能重复做它抛出405(不允许的方法)上RESTful WCF](http://stackoverflow.com/questions/17333013/jquery-ajax-post-request-throws-405-method-not-allowed-on-restful-wcf) – Sven

+0

调用external.net的提供者并询问他们将您添加到他们的CORS标头 – madalinivascu

+0

我需要执行请求而不能修改CORS h eader等..所以不,我不能“打电话给提供者”@madalinivascu –

回答

0

,如果你想从twitterjson数据,你必须为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>

+0

我需要向它发送POST请求,而不仅仅是获取页面。 –

+0

注册API密钥,以便他们将标题设置为'允许您的网站' – EaBangalore

+0

以其他方式使用'cURL',您可以登录到其他网站这是'POST'的示例 – EaBangalore

相关问题