POST请求后,AJAX读取ETag

时间:2013-10-26 11:27:44

标签: jquery ajax rest

我想知道,如何读取附加到服务器响应的ETag,该请求是通过ajax发生的客户端POST请求。目前我从客户端这样发帖,我从服务器得到了响应的实际主体:

$.ajax({
        type: "POST",
        accepts: "text/plain",
        url: "http://localhost:3000/somewhere",
        data: JSON.stringify(someObject),
        contentType: "application/json; charset=UTF-8", 
        dataType: "text",
        success: function(data) {
            window.alert("Received back: '" + data + "'");
        },
        username: theUsername,
        password: thePassword
    });

服务器通过设置如下标题来响应POST请求:

res.writeHead (200, {"ETag": "\"" + anETag + "\"", "Content-Type": "text/plain"});

提前感谢您的时间。

1 个答案:

答案 0 :(得分:2)

查看Get response header jquery ajax post Set-Cookie

基本上,成功函数中的第三个参数是XHR请求:

...
success: function (data, textStatus, xhr) {
    console.log(xhr.getResponseHeader('ETag'));
}
...