I'm writing some code to handle sending post data for my application and I wanted to make it so that I can send custom headers from another function if I need them. My question is, can I default something like "Content-Type" the way my code example does below and then overwrite it, or do I need to check the custom headers being sent, and if Content-type is not set, set it to the default. Basically, during the creation of the post request, can you overwrite headers programmatically?
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", formData.length);
// check for custom headers
if ((headers !== null) && (headers !== undefined)) {
for(var k in headers) {
if(headers.hasOwnProperty(k) {
xmlhttp.setRequestHeader(k.toString(), headers[k]);
}
}
}
I'm sending a different "Content-Type" like JSON perhaps in the "headers" object. If I do setRequestHeader on Content-Type again does it overwrite or does it send 2 content-type headers in the post request?
edit: I don't know why I asked this on StackOverflow, I just realized I could probably test this by logging my headers with a form handler, which I'm off to do, I'll leave the question up anyway.
for..inloops and not usinghasOwnProperty