2017-07-30 30 views
2
$.ajax({ 
     type  : 'POST', // define the type of HTTP verb we want to use (POST for our form) 
     url   : 'https://your_url.aspx', // the url where we want to POST 
     data  : school, // our data object 
     dataType : 'json', // what type of data do we expect back from the server 
        encode   : true 
    }) 
     // using the done promise callback 
     .done(function(data) { 

      // log data to the console so we can see 
      console.log(data); 

      // here we will handle errors and validation messages 
     }); 

    // stop the form from submitting the normal way and refreshing the page 
    event.preventDefault(); 

這是我的代碼,以某種形式的數據上傳到特定的網址張貼,但我收到以下錯誤:訪問控制允許來源的錯誤,而在阿賈克斯

POST https://your_url.aspx 500 (Internal Server Error) 
send @ jquery-3.2.1.min.js:4 
ajax @ jquery-3.2.1.min.js:4 
(anonymous) @ magic.js:20 
dispatch @ jquery-3.2.1.min.js:3 
q.handle @ jquery-3.2.1.min.js:3 
index.html:1 XMLHttpRequest cannot load 
https://your_url.aspx. No 'Access-Control- 
Allow-Origin' header is present on the requested resource. Origin 'null' is 
therefore not allowed access. The response had HTTP status code 500. 

我環顧四周對於如何讓訪問控制允許來源和$就

beforeSend: function(request){ 
    request.setHeaderRequest("Access-Control-Allow-Origin","*") 
} 

也計算器看到更多的答覆後,後來我改變setHeaderRequest到addHeaderReuest使用,但即使如此,我沒能看到響應從ser ver,雖然錯誤已被刪除。控制檯顯示爲空白。

任何幫助,將不勝感激。

回答

1

出現此錯誤是因爲您試圖從您的域中訪問另一個域。使用同源策略,我們只能請求存在於同一個域中的那些url。 跨域請求一般稱爲CORS只有在我們從其他站點進行身份驗證之前,瀏覽器才允許使用。 訪問控制 - 允許來源響應標頭指示響應是否可以與具有給定源的資源共享。檢查文檔Access-Control-Allow-Origin

+0

寫crossDomain:true也沒有幫助。 –

+0

不,在這裏你cleraly獲取錯誤消息作爲'允許起源'標題出現在所請求的資源。' –

+0

您想要擊中的另一個網站可能不允許跨域請求 –

相關問題