如何在钛中设置请求 headers

How to set request headers in titanium

有什么方法可以在钛中设置请求 headers 吗?

例如:API = abcdefgb56432142345234534;

我的 xhr:

var url = "http://www.appcelerator.com";
var client = Ti.Network.createHTTPClient({
    // function called when the response data is available
    onload: function(e) {
        Ti.API.info("Received text: " + this.responseText);
        alert('success');
    },
    // function called when an error occurs, including a timeout
    onerror: function(e) {
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();

是的,试试下面的方法,

打开请求后始终设置

var url = "http://www.appcelerator.com";
var client = Ti.Network.createHTTPClient({
    // function called when the response data is available
    onload: function(e) {
        Ti.API.info("Received text: " + this.responseText);
        alert('success');
    },
    // function called when an error occurs, including a timeout
    onerror: function(e) {
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);

client.setRequestHeader('API','abcdefgb56432142345234534'); //allways set after open
// Send the request.
client.send();