json - Application of the Accept Header in javascript -
i'm trying insert take header in json file. don't know why test not work. can help me? here code:
function dojsonrequest(type, url, headers, data, callback){ var r = new xmlhttprequest(); if(type && url && headers && callback && (arguments.length == 5)){ r.open(type, url, true); if((type != "get") && (type != "post") && (type != "put") && (type != "delete")){ throw new error('the value not recognized') } else if((type == "post") || (type == "put") || (type == "get") || (type == "delete")) { seek { if(data === undefined) { info = null; } else if(typeof info === 'object'){ r.send(json.stringify(data)); } else { throw new error("the info undefined") } } grab (e) { throw new error("the info has no json format") } } else if((type == "post") || (type == "put")){ r.setrequestheader("content-type","application/json"); r.send(json.stringify(data)); } and here test use:
var expectedgetheader = { "accept": "application/json" } dojsonrequest("get", baseurl, headers, null, callback1); settimeout(function(){ qunit.start(); assert.deepequal(requests[4].requestheaders, expectedgetheader, "should set \"accept\": \"application/json\" header requests"); },100); can help me passing it? specific error is:
should set "accept": "application/json" header requests expected: { "accept": "application/json" } result: {} diff: { "accept": "application/json" } {} solution adding line of code after opening r.setrequestheader("accept", "application/json");
i don't see accept header getting set anywhere. maybe right before calling r.send(json.stringify(data));, so:
if(data === undefined) { info = null; } else if(typeof info === 'object'){ r.setrequestheader('accept', 'application/json'); // <-- added r.send(json.stringify(data)); } else { throw new error("the info undefined") } javascript json
No comments:
Post a Comment