I want to access twitter API through ajax call but have no idea how to pass consumer key and consumer secret in ajax header.

var settings = {
“async”: true,
“crossDomain”: true,
“url”: “https://api.twitter.com/1.1/trends/available.json”,
“method”: “GET”,
“headers”: {
“Content-Type”: “application/json”,
“consumer_key”:“key”,

}
}

$.ajax(settings).done(function (response) {
console.log(response);
});

The Twitter API does not support cross-origin (CORS) requests so this is not possible.

then how should I use Twitter API using Ajax??

You need to use some kind of server-side proxy. You could look at something like this jQuery plugin as an example.

1 Like