Problems with xhr.setRequestHeader() and content-type
I am trying to talk with an api over post and json. But its keep sending the content-type asapplication/x-www-form-urlencoded; charset=utf-8
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function()
{
Ti.API.info('in utf-8 onload for POST');
Ti.API.info(this.responseText);
Ti.API.info(this.status);
Ti.API.info(this.connectionType);
};
xhr.onerror = function()
{
Ti.API.info('in utf-8 error for POST');
};
xhr.open("POST", "http://url.tld/api_jsonrpc.php");
xhr.setRequestHeader("Content-Type", "application/json-rpc");
xhr.send({
'method': 'user.authenticate',
'jsonrpc': '2.0',
'params': {"user": "demo", "password": "demo"},
'auth': false,
'id' : 1
});
Do any have a reason for that?
2 Answers
-
Accepted Answer
Configuration related settings for XHR should come before the open method is called and this includes setting the timeout as well. If you try to do an open and then change the settings they will not apply to the connection that was previously been opened.
This should work
xhr.setRequestHeader("Content-Type", "application/json-rpc"); xhr.open("POST", "http://url.tld/api_jsonrpc.php");
-
try xhr.setRequestHeader('Content-Type', 'application/json);