0

I have a Java server that I am sending application/json POST request to and returning 204 No Context Responses with 2 headers at a certain endpoint. But in my Javascript code I don't get the headers. This is cross-origin

When I test in Postman I am able to see these two headers, but in JavaScript (using Ajax and jQuery) I get a 204 Response with no headers. My Java server receives the request and processes it normally.But somehow the Javascript does not get headers. This is cross-origin

Update:Same thing happens with 200 Response. Postman gets the headers but not Javascript

 $.ajax({ 'type': 'POST', 'url': url, 'contentType': 'application/json', 'data': JSON.stringify(data), 'dataType': 'json', success:login }); 

In the login function I try to log some values

function login(data, statusText, response) { var status = response.status; console.log(status) var headers = response.getAllResponseHeaders(); console.log(response) console.log(headers) var transID = response.getResponseHeader("transactionID"); console.log(transID) } 

Status correctly shows 204, headers is an empty string, and transID is null. I am confident the Java server is putting the headers in the response because my Postman tests display them

The url is the Java server running on localhost so I didn't include it

5
  • 2
    You've not included the code we need to see. Please show the code in the login() where you attempt to read these headers. Commented May 24, 2019 at 8:24
  • 1
    Your JavaScript goes isn't trying to access any headers. You haven't said if this is same-origin or cross-origin. You should provide a minimal reproducible example Commented May 24, 2019 at 8:24
  • "somehow the Javascript does not get headers" ...you haven't shown us any code which attempts to read the headers. Where is that code? If you show us, maybe we can help you fix it. Commented May 24, 2019 at 8:55
  • my bad, I should've mentioned I was using getAllResponseHeaders() and getResponseHeader("name") to find the headers. So far these are the only methods I have found. Commented May 24, 2019 at 10:11
  • stackoverflow.com/questions/32103768/… and stackoverflow.com/questions/31521793/… and stackoverflow.com/questions/6112524/… might all be useful things to read. In fact there's quite a lot of material about this scenario available if you search. P.S. You can use your browser's Developer tools to see what is actually being returned in the headers of the request. Commented May 24, 2019 at 10:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.