2016-04-27 142 views
0
curl --basic --user {username}:{password} https://www.blueworkslive.com/api/Auth 

我試圖寫這樣的東西,但它不會工作!我不斷收到錯誤400將cURL API調用轉換爲Ajax

$.ajax({ 
    url: "https://www.blueworkslive.com/api/Auth", 
    type: "GET", //This is what you should chage 
    dataType: "jsonp", 
    username: "admin", // Most SAP web services require credentials 
    password: "admin", 
    processData: false, 
    contentType: "application/json; charset=utf-8", 
    success: function() { 
     alert("success"); 
     console.log("success"); 
    }, 
    error: function (xhr, ajaxOptions, thrownError) { 
     console.log("error"); 
     console.log(xhr.responseText); 
     alert(xhr.status); 
     alert(xhr.responseText); 
    }, 
}); 

而這也: {$(文件)。就緒(函數(){VAR XHR =新的XMLHttpRequest();

 xhr.open("GET", "https://www.blueworkslive.com/api/Auth?version=20091212", false); 
     xhr.setRequestHeader("Authorization", "Basic " + btoa("username.com:password")); 
     xhr.setRequestHeader("Content-Type", "application/json"); 
    xhr.setRequestHeader("Access-Control-Allow-Origin: *"); 
     xhr.send(""); 

     alert(xhr.status); 
     alert(xhr.responseText); 

});} 錯誤:Access-Control-Allow-Origin不允許來源。我認爲錯誤是因我引起的從本地服務器上運行腳本(因爲原點設置爲默認爲空),所以我部署它的應用開發服務器上,以便我有一個公共origin.The的發生同樣的錯誤新的起源 - 即使現在的起源不再爲空,但服務器提供了起源!

+0

@ blackpla9ue您是否在編輯時添加了評論? – devnull69

+0

@ devnull69 nope。只有縮進。檢查編輯歷史。我嘗試期間 – blackpla9ue

+0

有趣......要編輯我提交了「您的」編輯顯示添加註釋,現在我可以從歷史中看到,這種變化是 – devnull69

回答

0
$(document).ready(function(){ 
      $(function(){ 
       $.ajax({ 
        url: 'https://www.blueworkslive.com/api/Auth', // url checked ...// error 400 not found url 
        data: { 
         //data 
         username:username, 
         email:email 
        }, 
        dataType: 'json', 
        success: function(data) 
        { 
         //result success 
        } 
       }); 
      }); 
     }); 
+0

請求的資源上沒有「Access-Control-Allow-Origin」頭。因此不允許原產地'null'訪問。該響應具有HTTP狀態代碼400。 – Radu