Can't Get Custom HTTP Header Response from Ajax getAllResponseHeaders

后端 未结 1 550
小鲜肉
小鲜肉 2020-12-08 10:23

I do get the response data, but I can\'t get my custom HTTP header data.

Yes, this is a cross-domain request. I am doing an Ajax request with Javascript. I\'ve tried

相关标签:
1条回答
  • 2020-12-08 11:01

    I wanna thank jbl for pointing me to the right SO question. I got it now...

    So, OK... the answer. If you ever wanted to set your own HTTP Header information, and then fetch it using cross-domain Ajax, or something like that, here are some extra HTTP Header you should set on your server side, before sending the response text.

    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Methods: POST, GET");      
    header('Custom-Header: Own-Data');
    header('Access-Control-Expose-Headers: Custom-Header');
    

    Example above uses PHP. But use your own language, what ever you use to set them.

    When I asked this question, I had all of that except Access-Control-Expose-Headers. After putting that in, my Javascript Ajax can read the content of HTTP Header Custom-Header.

    0 讨论(0)
提交回复
热议问题