2016-11-22 83 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

相關問題