導航:首頁 > 編程語言 > js獲取responseheader

js獲取responseheader

發布時間:2023-03-18 13:55:56

『壹』 js如何獲取response header信息

普通的請求JS無法獲取,,只有ajax請求才能獲取到。

『貳』 js獲取頁面response headers中各domain下的cookie個數、及總的cookie大小 跪求啊

js 只能來在當前域名下進行操源作,不能跨域的.

如: www.qq.com
game.qq.com
web.qq.com
......
採用的域名為 qq.com

你是想搞破壞嗎?

『叄』 js 中怎麼獲取當前系統時間

系統時間一般是值服務端時間,js獲取服務端時間的方法是直接用ajax獲取。

編寫顯示時間的頁面:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Server date/time</title>

<script language="javascript" src="serverDate.js"></script>

</head>

<script language="javascript">

var localTime = new Date();

document.write("Local machine time is: " + localTime + "<br>");

document.write("Server time is: " + date);

</script>

<body>

</body>

ajax腳本獲取server的時間

var xmlHttp;

function srvTime(){

try {

//創建xmlHttp對象

xmlHttp = new XMLHttpRequest();

}

catch (err1) {

//ie瀏覽器

try {

xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');

}

catch (err2) {

try {

xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');

}

catch (eerr3) {

//ajax不支持

alert("AJAX not supported");

}

}

}

//打開xmlHttp請求

xmlHttp.open('HEAD',window.location.href.toString(),false);

//設置xmlHttp請求頭

xmlHttp.setRequestHeader("Content-Type", "text/html");

//發送請求

xmlHttp.send('');

// 獲取response中的Date參數

return xmlHttp.getResponseHeader("Date");

}

var st = srvTime(); //伺服器時間賦值給st變數

var date = new Date(st); //轉換js的date對象

// 輸出伺服器時間

document.write("伺服器時間: " + date);

拓展資料:

JavaScript一種直譯式腳本語言,是一種動態類型、弱類型、基於原型的語言,內置支持類型。它的解釋器被稱為JavaScript引擎,為瀏覽器的一部分,廣泛用於客戶端的腳本語言,最早是在HTML(標准通用標記語言下的一個應用)網頁上使用,用來給HTML網頁增加動態功能。

語句:JavaScript程序是由若干語句組成的,語句是編寫程序的指令。JavaScript提供了完整的基本編程語句,

它們是:賦值語句、switch選擇語句、while循環語句、for循環語句、for each循環語句、do...while循環語句、break循環中止語句、continue循環中斷語句、with語句、try?catch語句、if語句(if..else,if?else if?)。

『肆』 javascript如何獲得後台response.getWriter().write()返回的值我用的struts2

這個你要用抄ajax才可以獲得。
也就是說,在js里
1新建XMLHttpRequest對象(例如名為:xmlhttp)
2.XMLHttpRequest對象發送請求 :xmlhttp.open("GET","url",true);
注意裡面的url就是你struts2跳轉的action,例如test.action
3.通過struts.xml找到url跳轉到的Action,在execute方法里寫 response.getWriter().write("aaaaa");
Struts2獲得Httpresponse對象會吧,要用ServletActionContext.getResponse(); (別忘了在execute方法最後加上一句return null ,作為execute方法的返回。)
4.在js後面里繼續寫: var resultstr = xmlhttp.responseText; 那麼等非同步傳輸完成,resultstr就會被賦值為"aaaaa"

我前面說的1,2,4步驟都是涉及ajax內容,如果對ajax不了解,需要先去學習ajax基礎用法

『伍』 node.js里Response headers里中文亂碼怎麼解決

res.setHeader('Content-Type','text/javascript;charset=UTF-8');
記得在send()之前~
用decodeURIComponent(escape(s))解碼

『陸』 可以在本地的html文件中寫js調用HTTP請求嘛,如果可以請大神寫出具體的js代碼

是可以的 如下

<html>
<head>
<title>XMLHTTPRequest對象的說明DEMO</title>
<script language="javascript" type="text/javascript">
<!--
var xmlhttp;
// 創建一個XMLHTTPRequest對象
function createXMLHTTPRequext(){
if(window.ActiveXObject) {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
else if(window.XMLHTTPRequest){
xmlhttp = new XMLHTTPRequest();
}
}
function PostOrder(xmldoc)
{
alert(xmldoc);
createXMLHTTPRequext();

// 方法:open
// 創建一個新的http請求,並指定此請求的方法、URL以及驗證信息
// 語法:oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
// 參數
// bstrMethod
// http方法,例如:POST、GET、PUT及PROPFIND。大小寫不敏感。
// bstrUrl
// 請求的URL地址,可以為絕對地址也可以為相對地址。
// varAsync[可選]
// 布爾型,指定此請求是否為非同步方式,默認為true。如果為真,當狀態改變時會調用onreadystatechange屬性指定的回調函數。
// bstrUser[可選]
// 如果伺服器需要驗證,此處指定用戶名,如果未指定,當伺服器需要驗證時,會彈出驗證窗口。
// bstrPassword[可選]
// 驗證信息中的密碼部分,如果用戶名為空,則此值將被忽略。

// 備註:調用此方法後,可以調用send方法向伺服器發送數據。
xmlhttp.Open("get", xmldoc, false);
// var book = xmlhttp.responseXML.selectSingleNode("//book[@id='bk101']");
// alert(book.xml);

// 屬性:onreadystatechange
// onreadystatechange:指定當readyState屬性改變時的事件處理句柄
// 語法:oXMLHttpRequest.onreadystatechange = funcMyHandler;
// 如下的例子演示當XMLHTTPRequest對象的readyState屬性改變時調用HandleStateChange函數,
// 當數據接收完畢後(readystate == 4)此頁面上的一個按鈕將被激活
// 備註:此屬性只寫,為W3C文檔對象模型的擴展.
xmlhttp.onreadystatechange= HandleStateChange;

// 方法:send
// 發送請求到http伺服器並接收回應
// 語法:oXMLHttpRequest.send(varBody);
// 參數:varBody (欲通過此請求發送的數據。)
// 備註:此方法的同步或非同步方式取決於open方法中的bAsync參數,如果bAsync == False,此方法將會等待請求完成或者超時時才會返回,如果bAsync == True,此方法將立即返回。
// This method takes one optional parameter, which is the requestBody to use. The acceptable VARIANT input types are BSTR, SAFEARRAY of UI1 (unsigned bytes), IDispatch to an XML Document Object Model (DOM) object, and IStream *. You can use only chunked encoding (for sending) when sending IStream * input types. The component automatically sets the Content-Length header for all but IStream * input types.
// 如果發送的數據為BSTR,則回應被編碼為utf-8, 必須在適當位置設置一個包含charset的文檔類型頭。
// If the input type is a SAFEARRAY of UI1, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
// 如果發送的數據為XML DOM object,則回應將被編碼為在xml文檔中聲明的編碼,如果在xml文檔中沒有聲明編碼,則使用默認的UTF-8。
// If the input type is an IStream *, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
xmlhttp.Send(xmldoc);

// 方法:getAllResponseHeaders
// 獲取響應的所有http頭
// 語法:strValue = oXMLHttpRequest.getAllResponseHeaders();
// 備註:每個http頭名稱和值用冒號分割,並以\r\n結束。當send方法完成後才可調用該方法。
//alert(xmlhttp.getAllResponseHeaders());
// 方法:getResponseHeader
// 從響應信息中獲取指定的http頭
// 語法:strValue = oXMLHttpRequest.getResponseHeader(bstrHeader);
// 備註:當send方法成功後才可調用該方法。如果伺服器返回的文檔類型為"text/xml", 則這句話
// xmlhttp.getResponseHeader("Content-Type");將返回字元串"text/xml"。可以使用getAllResponseHeaders方法獲取完整的http頭信息。
//alert(xmlhttp.getResponseHeader("Content-Type")); // 輸出http頭中的Content-Type列:當前web伺服器的版本及名稱。

//document.frmTest.myButton.disabled = true;
// 方法:abort
// 取消當前請求
// 語法:oXMLHttpRequest.abort();
// 備註:調用此方法後,當前請求返回UNINITIALIZED 狀態。
// xmlhttp.abort();

// 方法:setRequestHeader
// 單獨指定請求的某個http頭
// 語法:oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);
// 參數:bstrHeader(字元串,頭名稱。)
// bstrValue(字元串,值。)
// 備註:如果已經存在已此名稱命名的http頭,則覆蓋之。此方法必須在open方法後調用。
// xmlhttp.setRequestHeader(bstrHeader, bstrValue);
}
function HandleStateChange()
{
// 屬性:readyState
// 返回XMLHTTP請求的當前狀態
// 語法:lValue = oXMLHttpRequest.readyState;
// 備註:變數,此屬性只讀,狀態用長度為4的整型表示.定義如下:
// 0 (未初始化) 對象已建立,但是尚未初始化(尚未調用open方法)
// 1 (初始化) 對象已建立,尚未調用send方法
// 2 (發送數據) send方法已調用,但是當前的狀態及http頭未知
// 3 (數據傳送中) 已接收部分數據,因為響應及http頭不全,這時通過responseBody和responseText獲取部分數據會出現錯誤,
// 4 (完成) 數據接收完畢,此時可以通過通過responseBody和responseText獲取完整的回應數據
if (xmlhttp.readyState == 4){
document.frmTest.myButton.disabled = false;

// 屬性:responseBody
// 返回某一格式的伺服器響應數據
// 語法:strValue = oXMLHttpRequest.responseBody;
// 備註:變數,此屬性只讀,以unsigned array格式表示直接從伺服器返回的未經解碼的二進制數據。
//alert(xmlhttp.responseBody);

// 屬性:responseStream
// 以Ado Stream對象的形式返回響應信息
// 語法:strValue = oXMLHttpRequest.responseStream;
// 備註:變數,此屬性只讀,以Ado Stream對象的形式返回響應信息。
//alert(xmlhttp.responseStream);

// 屬性:responseText
// 將響應信息作為字元串返回
// 語法:strValue = oXMLHttpRequest.responseText;
// 備註:變數,此屬性只讀,將響應信息作為字元串返回。XMLHTTP嘗試將響應信息解碼為Unicode字元串,
// XMLHTTP默認將響應數據的編碼定為UTF-8,如果伺服器返回的數據帶BOM(byte-order mark),XMLHTTP可
// 以解碼任何UCS-2 (big or little endian)或者UCS-4 數據。注意,如果伺服器返回的是xml文檔,此屬
// 性並不處理xml文檔中的編碼聲明。你需要使用responseXML來處理。
// alert(xmlhttp.responseText);

// 屬性:responseXML
// 將響應信息格式化為Xml Document對象並返回
// 語法:var objDispatch = oXMLHttpRequest.responseXML;
// 備註:變數,此屬性只讀,將響應信息格式化為Xml Document對象並返回。如果響應數據不是有效的XML文檔,
// 此屬性本身不返回XMLDOMParseError,可以通過處理過的DOMDocument對象獲取錯誤信息。
//alert("Result = " + xmlhttp.responseXML.xml);

// 屬性:status
// 返回當前請求的http狀態碼
// 語法:lValue = oXMLHttpRequest.status;
// 返回值:長整形標准http狀態碼,定義如下:
// Number:Description
// 100:Continue
// 101:Switching protocols
// 200:OK
// 201:Created
// 202:Accepted
// 203:Non-Authoritative Information
// 204:No Content
// 205:Reset Content
// 206:Partial Content
// 300:Multiple Choices
// 301:Moved Permanently
// 302:Found
// 303:See Other
// 304:Not Modified
// 305:Use Proxy
// 307:Temporary Redirect
// 400:Bad Request
// 401:Unauthorized
// 402:Payment Required
// 403:Forbidden
// 404:Not Found
// 405:Method Not Allowed
// 406:Not Acceptable
// 407:Proxy Authentication Required
// 408:Request Timeout
// 409:Conflict
// 410:Gone
// 411:Length Required
// 412:Precondition Failed
// 413:Request Entity Too Large
// 414:Request-URI Too Long
// 415:Unsupported Media Type
// 416:Requested Range Not Suitable
// 417:Expectation Failed
// 500:Internal Server Error
// 501:Not Implemented
// 502:Bad Gateway
// 503:Service Unavailable
// 504:Gateway Timeout
// 505:HTTP Version Not Supported
// 備註:長整形,此屬性只讀,返回當前請求的http狀態碼,此屬性僅當數據發送並接收完畢後才可獲取。
alert(xmlhttp.status);

// 屬性:statusText
// 返回當前請求的響應行狀態
// 語法:strValue = oXMLHttpRequest.statusText;
// 備註:字元串,此屬性只讀,以BSTR返回當前請求的響應行狀態,此屬性僅當數據發送並接收完畢後才可獲取。
//alert(xmlhttp.statusText);
}
}
//-->
</script>
</head>
<body>
<form name="frmTest">
<input name="myButton" type="button" value="Click Me" onclick="PostOrder('http://www.eji.com');">
</form>
</body>
</html>

『柒』 js怎麼獲取介面的headers的status code信息

.先實現一個對Response的包裝器:

Java代碼
public class StatusExposingServletResponse extends HttpServletResponseWrapper {

private int httpStatus;

public StatusExposingServletResponse(HttpServletResponse response) {
super(response);
}

@Override
public void sendError(int sc) throws IOException {
httpStatus = sc;
super.sendError(sc);
}

@Override
public void sendError(int sc, String msg) throws IOException {
httpStatus = sc;
super.sendError(sc, msg);
}


@Override
public void setStatus(int sc) {
httpStatus = sc;
super.setStatus(sc);
}

public int getStatus() {
return httpStatus;
}

}

2.然後實現一個Filter來替換原始的HttpServletResponse,這樣你就可以在Filter裡面取到statusCode了

Java代碼
public class StatusReportingFilter implements Filter {

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
StatusExposingServletResponse response = new StatusExposingServletResponse((HttpServletResponse)res);
chain.doFilter(req, response);
int status = response.getStatus();
// report
}

public void init(FilterConfig config) throws ServletException {
//empty
}

public void destroy() {
// empty
}

}

『捌』 js怎麼獲得用ajax返回的response內容

請求的時候多加一個參數
dataType:json
返回的就是array數組。
兩個response,在兩個不同的success方法裡面,作用域是不同的,沒毛病

『玖』 js怎樣獲得 response header 數據為什麼都是null

jqXHR有個getResponseHeader()方法,參考代碼:$.ajax({success:function(data,status,xhr){console.log(xhr.getResponseHeader("Content-Type"));}});

『拾』 如何獲取跨域請求的自定義response headers

AngularJS XMLHttpRequest:$http用於讀取遠程伺服器的數據
$http.post(url, data, [config]).success(function(){ ... });
$http.get(url, [config]).success(function(){ ... });
$http.get(url, [config]).success(function(){ ... });
一、$http.jsonp【實現跨域】
1. 指定callback和回調函數名,函數名為JSON_CALLBACK時,會調用success回調函數,JSON_CALLBACK必須全為大寫。
2. 指定其它回調函數,但必須是定義在window下的全局函數。url中必須加上callback。
二、$http.get【實現跨域】
1. 在伺服器端設置允許在其他域名下訪問
response.setHeader("Access-Control-Allow-Origin", "*"); //允許所有域名訪問
response.setHeader("Access-Control-Allow-Origin", ""); //允許www.123.com訪問
2. AngularJS端使用$http.get()
三、$http.post【實現跨域】
1. 在伺服器端設置允許在其他域名下訪問,及響應類型、響應頭設置
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods","POST");
response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
2. AngularJS端使用$http.post(),同時設置請求頭信息
$http.post('',{languageColumn:'name_eu'},{'Content-Type':'application/x-www-form-urlencoded'}).success(function(data){
$scope.instries = data;
});

閱讀全文

與js獲取responseheader相關的資料

熱點內容
編程怎麼與steam教育融合 瀏覽:697
js製作滑鼠拖拽小塊 瀏覽:310
將圖紙拆分為多個CAD文件 瀏覽:779
如何鑒別dsd文件 瀏覽:902
thinkphp不能用js 瀏覽:664
蘋果11粘膩app是什麼意思 瀏覽:670
安卓手機中木馬了怎麼辦 瀏覽:964
java組建模型 瀏覽:53
wifi萬能密碼安全嗎 瀏覽:785
紅色系圓圈是什麼app 瀏覽:714
迷你編程開始的教程怎麼過 瀏覽:216
上海國衡網站有什麼用 瀏覽:29
掃描文件如何全選 瀏覽:363
directx一鍵修復工具 瀏覽:620
如何恢復谷歌同步中刪除的文件夾 瀏覽:215
安卓51轉換為系統應用 瀏覽:789
哪裡看雙11數據 瀏覽:783
文件變成exe如何恢復 瀏覽:49
為什麼逆戰會缺少文件 瀏覽:180
蘋果4s版本怎麼更新 瀏覽:418

友情鏈接