2017-10-05 35 views
0

我想用jQuery發送一個AJAX請求。 調用網址需要授權。直接在瀏覽器中輸入URL,會彈出瀏覽器的授權/登錄對話框。 如何使用此憑據或強制使用jQuery彈出對話框?帶瀏覽器認證的jQuery AJAX調用

$.ajax({ 
 
    type: "GET", 
 
    url: "myURL", 
 
    data: { param1: "1"}, \t \t \t 
 
    contentType: 'application/json', 
 
    dataType: 'json', 
 
    success: function(data, textStatus, jQxhr){ \t \t \t \t 
 
\t  console.log(data); \t \t 
 
    }, 
 
    error: function(jqXhr, textStatus, errorThrown){ \t \t \t \t 
 
     console.log(errorThrown); \t \t 
 
    } 
 
});

回答

0

渲染你的同一個頁面在身份驗證裏面的iframe失敗。

var url = "your website url here" 

$.ajax({ 
    type: "GET", 
    url: "myURL", 
    data: { param1: "1"},  
    contentType: 'application/json', 
    dataType: 'json',  
    success: function(data, textStatus, jQxhr){  
     console.log(data);  
    }, 
    error: function(jqXhr, textStatus, errorThrown){   
     if(jqXhr.status==401){   
      $('<iframe src="'+url+'"></iframe>').appendTo('body');   
     } 
    } 
}); 
+0

但爲此,我需要用戶名和密碼。我怎樣才能從默認瀏覽器的對話框中得到這個(並防止編寫我自己的登錄頁面)? – Konrad

+0

我沒有得到你的問題。你能澄清嗎? – krishnar

+0

在瀏覽器中調用需要授權的URL時,瀏覽器使用其默認驗證對話框要求驗證。如果一個網站通過.htaccess安全。 ...我想爲我的ajax調用使用此憑據...並且我不想再次提示/詢問用戶(並使用我自己的登錄頁面)以獲取他的憑據。 – Konrad