如何从ajax post请求获取响应头

时间:2017-04-19 15:15:22

标签: javascript ajax cors isomorphic-fetch-api

我想检索服务器发送的响应标头。但我得到空的对象。我可以在chrome dev工具中看到响应标头,但无法通过javascript获取它们。

(我正在为xhr请求使用isomorphic-fetch lib。)

        fetch("http://my.cors.url/postsomedata", {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(myjsobject),
        })
        .then(response => response.headers)
        .then((headers) => {
            console.log(headers); // empty object
        })

这不是公告的 How to get Response headers in AJAX 我没有使用jQuery。

1 个答案:

答案 0 :(得分:0)

问题出在我的后端服务器CORS配置上。 我正在使用spring boot,所以为了将自定义标头传递给xhr响应,我需要按照以下方式配置spring boot应用程序

array=[
       {
        "name":"siddhesh",
        "mobile":[{"fm":"83******","lm":"78******"}]
       }
     ];