Can you add headers to getJSON in jQuery?
$.getJSON is a shorthand for
$.ajax({ dataType: "json", url: url, data: data, success: success});
So you can simply use it directly like
$.ajax({ beforeSend: function(request) { request.setRequestHeader("X-Mashape-Key", 'key_here'); }, dataType: "json", url: settings.apiPath + settings.username + '/boards/', success: function(data) { //Your code }});
using $.ajax() would have been better, as $.getJSON
is shorthand function, if you cant use $.ajax()
, you can use $.ajaxSetup to set headers, as:
$.ajaxSetup({ headers : { 'X-Mashape-Key' : 'key_here' }});$.getJSON(settings.apiPath + settings.username + '/boards/', function (data) { ...
But note that it sets headers for future Ajax requests.
Since getJSON is a shortcut notation for $.ajax() why not use that
$.ajax({ dataType: "json", url: url, data: data, success: success});
ajax()
provides a hook called which you can specify by beforeSend: function(jqXHR, settings)
The hook allows one to add custom headers to outgoing AJAX request ... I think unlike $.ajaxSetup
... you do not have to worry about unsetting after using it