我可以在瀏覽器中傳遞重定向url和用戶id的參數,這是一個intranet應用程序btw。因此,您可以粘貼地址,例如「http:// intranetapp?redirect_url = http:// crapola & userid = xxxxxxx。它將您重定向到該網址並提供用戶ID的其他信息,這是我想要獲取的有幾百個用戶,這些信息作爲你重定向的參數的一部分被返回,有沒有辦法用jQuery或jQuery的相關方法調用(GET請求),讀取返回的url和參數,而不是僅僅獲取返回的html?使用jquery ajax從重定向請求中讀取url參數
回答
克里斯,如果我理解正確的話,你想要做的是適度棘手。
我從來沒有需要做這種事情,但原則上知道它涉及一種不尋常的ajax請求類型 - 即「HEAD」請求,允許檢查重定向url(和其他元數據)而不接收HTTP響應的主要部分(正文)。
您的Intranet服務器應該處理HEAD請求(它們至少與GET一樣安全),但不一定。如果沒有,那麼請與你的服務器管理員說一句話。如果你是服務器管理員,那麼在httpd.conf
文件和/或相應的.htaccess
文件(假設爲Apache)中有一個根。
與所有類型的ajax一樣,代碼也很棘手,因爲它的一部分需要異步運行(當服務器的HTTP響應返回時)。爲了解決這個問題,jQuery的Deferreds/Promises可以(寬鬆地)使用。
你的主要工作器功能(仍然如果我理解正確的)會是這樣的:
function getUserParams(userID) {
var $ = jQuery,
dfrd = $.Deferred(),
q = {},
baseURL = 'http://intranetapp?redirect_url=http://crapola&userid=';
$.ajax({
type: "HEAD",
url: baseURL + userID,
cache: false,
success: function(data, textStatus, jqXHR) {
var location = jqXHR.getResponseHeader('Location');
if(location){
var search = $("<a>").attr('href', location).get(0).search.replace(/^[?]/, ''),
prop, pair;
if (search) {
$.each(search.split("&"), function(i, arg) {
pair = arg.split("=");
if (pair.length >= 1) {
prop = pair.shift();
q[prop] = (pair.length == 1) ? pair[0] : (pair.length > 1) ? pair.join('=') : '';
}
});
}
//At this point q is a hash representing parameters in the location's search string.
dfrd.resolve(userID, q);
}
else {
dfrd.reject(userID, 'No redirect url in the response');
}
},
error: function(jqXHR, textStatus, errorThrown) {
dfrd.reject(userID, 'Ajax failure: ' + textStatus + ': ' + errorThrown);
}
});
return dfrd.promise();
}
需要注意的是,由於AJAX是異步的,我們回到一個承諾不是我們真正想要的結果;他們稍後到達,打包在javascript普通對象q
中。
下面是如何測試getUserParams()
:
var userID = '12345678';
getUserParams(userID).done(function(userID, q) {
//Work with userID and q as required
console.log(['Success', userID, q.fullname, q.status, q.postalcode].join(': '));//for example
}).fail(function(userID, message) {
//Handle error case here
message = message || '';
console.log(['Error', userID, message].join(': '));//for example
});
你的使用目的,與數百名的URL,將是這樣的:
var userIDs = [
//array of userIDs (hard coded or otherwise constructed)
'1234',
'5678'
];
var promises = [];
$.each(userIDs, function(i, userID) {
var p = getUserParams(userID).done(function(userID, q) {
//work with userID and q as required
$("<p/>").text([userID, q.fullname, q.status, q.postalcode].join(': ')).appendTo($("#results"));//for example
}).fail(function(userID, message) {
//handle error case here
message = message || '';
console.log(['Error', userID, message].join(': '));//for example
});
promises.push(p);
});
你也可以做一些事情時,應對ALL ajax請求已收到。如果是這樣,則額外的代碼看起來就像這樣:
$.when.apply(null, promises).done(function() {
//Do whatever is required here when ALL ajax requests have successfully responsed.
//Note: any single ajax failure will cause this action *not to happen*
//alert('All user data was gathered');
console.log('All user data was gathered');
}).fail(function() {
//Do whatever is required here when ALL ajax requests have responsed.
//Note: any single ajax failure will cause this action *to happen*
//alert('At least one set of user data failed');
console.log('At least one ajax request for user data failed');
}).then(function() {
//Do whatever is required here when ALL ajax requests have responsed.
//Note: This function will fire after either the done or fail function.
//alert('Gathering of user data complete, but not necessarily successfully');
console.log('Gathering of user data complete, but not necessarily successfully');
});
部分測試(代碼運行,但我沒有測試Location
頭的重定向或處理的手段)。
您需要微調代碼的某些子集才能精確使用q
對象中的用戶數據,並適當地處理錯誤。
此功能可以幫助
function getUrl() {
var args = {}; // Start with an empty <a title="object" href="http://muraliprashanth.me/category/javascript/object/">object</a>
var query = location.search.substring(1); // Get query string, minus '?'
var pairs = query.split('&'); // Split at ampersands
for(var i = 0; i < pairs.length; i++) { // For each fragment
var pos = pairs[i].indexOf('='); // Look for 'name=value'
if (pos == -1) continue; // If not found, skip it
var name = pairs[i].substring(0,pos); // Extract the name
var value = pairs[i].substring(pos+1); // Extract the value
value = decodeURIComponent(value); // Decode the value
args[name] = value; // Store as a property
}
return args; // Return the parsed arguments
}
由於我必須讀取重定向數百個重定向到轉址後的位置(url),我最好是創建一個帶有隱藏iframe的html頁面,而不是嘗試使用ajax嗎?嵌入式iframe的位置是否存在?我知道標準的JavaScript解析當前頁面的位置。問題是關於使用製作好的網址,需要到那裏並將重定向到我選擇的網址,然後才能通過網絡cgi爲我填充位置,該網址爲我提供信息並進行重定向。不幸的是,重定向不是可選的。 – chris
- 1. 如何使用請求中的參數處理重定向url?
- 2. 如何從重定向中讀取JSF中的GET請求參數
- 3. 從URL獲取參數並重定向?
- 4. 重定向ajax請求
- 5. Jmeter - 從重定向URL獲取參數
- 6. 重定向URL請求?
- 7. 通過jQuery的ajax請求重定向的url
- 8. 未從SOAP請求中讀取參數
- 9. 在Python中映射URL參數請求重定向
- 10. 將用戶從jquery ajax請求重定向到Login頁面
- 11. AJAX POST請求使用JQuery與正文和URL參數
- 12. jquery ajax請求中的URL
- 13. 重定向url使用ajax
- 14. JQuery - 獲取請求後重定向
- 15. 在POST請求中重定向到URL
- 16. jQuery中的url參數中的函數Ajax請求
- 17. 請求重定向使用
- 18. 如何使用AJAX請求獲取重定向網址?
- 19. 將JSF ajax請求重定向到包含servlet過濾器中的請求參數的URL
- 20. 重定向到URL請求與修改後的參數
- 21. 使用groovy從請求url soapui中獲取查詢參數
- 22. 302重定向與AJAX POST請求:
- 23. Ajax請求完成後重定向
- 24. GET請求中的特定參數:請勿301重定向
- 25. 從yii2獲取請求被重定向到的url?
- 26. 使用jQuery從AJAX請求中斷HTML
- 27. 動態獲取URL從數據JQuery的Ajax請求屬性
- 28. Safari Ajax cors請求不要重定向
- 29. 使用Ajax方法讀取POST請求
- 30. 301重定向使用URL參數
如果它們作爲HTTP標頭的一部分返回,[this](http://stackoverflow.com/questions/6696229/jquery-ajax-fetch-only-headers-and-decide-wether-to-get-內容)可能會有所幫助。 – ebrandell
我不知道我的理解,但也許這會有所幫助。當前頁面的查詢字符串('''右側的所有內容)可作爲'location.search'提供給javascript,它可以被解析出來以確定每個參數的名稱|值。 –