So I’m doing a GET with jQuery Ajax like this:
$.ajax({
//...
headers: {'a':{'t':'text'}, 'b': {'s':'text2'}}
//...
});
According to (my interpretation of) the jQuery docs, headers should go in as
a: {'t':'text'}
b: {'s':'text2'}
Instead, on firebug (and in Fiddler) I see:
a: [object Object]
b: [object Object]
Now if I pass it like this:
$.ajax({
//...
headers: {'a':JSON.stringify({t:'text'}), 'b': JSON.stringify({'s':'text2'})}
//...
});
firebug shows them as:
a: {'t':'text'}
b: {'s':'text2'}
JSON.stringify is from Douglas Crockford’s library.
The part I hate about the second approach is that now I need to loop thru my object, and stringify child objects. (Note, I have no idea what’s inside the object so I cannot do individual setHeader())
My question is: Is my understanding incorrect of how headers are parsed or am I overlooking something?
Also, I’m looking for the community’s inputs on efficiently looping thru the JSON object and stringifying the children.
Update: Its pretty clear that my initial understanding was incorrect.
Anyway, any inputs on how to traverse the object and JSON.stringify children efficiently?
Your initial understanding was incorrect. HTTP headers are strings such as:
and, as such, you must provide a string as the name and one as the value. Your corrected version where you stringify the children of the top-most object provides just such a string-name/string-value structure.