2014-08-30 80 views
-1

嗯,我找不到出路。我谷歌很多,嘗試不同的方式,代碼示例等等,我有這個奇怪的問題。 在SoapUI中,我可以測試Web服務,我甚至可以通過身份驗證來測試服務。 但是... 我不能通過代碼。AJAX + jQuery + SAP認證+基本認證= 401未經授權。 (代碼+圖片)

所以這裏有一些打印屏幕和我正在嘗試的代碼。 如果你們能幫助我。 我將不勝感激。

謝謝。

CODE:---------------

var WEBSERVICE_URL = "http : // webservice:port/bla/bla/bla"; 
    $.ajax({ 
     url: WEBSERVICE_URL, 
     type: "POST", 
     dataType: "json", 
     username: "user", 
     password: "password", 
     processData: false, 
crossDomain: true, 
     contentType: "application/json", 
beforeSend: function (xhr) { 
xhr.setRequestHeader ("Authorization", "Basic SomeLettersAndNumbers_I_GotBySoapUI_IKnowHereItGoesTheKeyString=="); 
}, 
     success: function() { 
      alert("success"); 
     }, 
     error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response 
      alert(xhr.status); 
      alert(xhr.responseText); 
     }, 
    }); 

image1

image2

回答

0
username "user", 
    password: "password", 

應該

data: { 
    username: "user", 
    password: "password" 
}, 

其中一個可能只是其中一個問題秒。我不確定

0

看起來您正在嘗試提出跨網站請求。我猜這導致JQuery將結果顯示爲401,這並不是特別有用,因爲實際上似乎發生的事情是請求從未將其發送到服務器。您可以通過在Chrome上使用一些特殊選項來解決此問題,但我認爲這不會在標準瀏覽器上運行,除非您可以讓服務器提供必要的訪問控制標頭。