jQuery ajax headers parsing issue
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 unders开发者_如何学Pythontanding 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 stringify
ing 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:
X-Some-Header: Some Value
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.
I think you're trying to use a JSON Object as your header values, but you need to send it as a string in that case:
$.ajax({
//...
headers: {'a': "{'t':'text'}", 'b': "{'s':'text2'}"}
//...
});
精彩评论