Use of $.ajax(); and $.ajaxSetup();

Example code


$(document).ready(function(){
    
		
		$.ajaxSetup({
						cache: false,
						dataType: "json",
						contentType: "application/x-www-form-urlencoded; charset=UTF-8",
						beforeSend: function (xhr) {
							xhr.setRequestHeader('bearer', 'BEARER_KEY_HERE')
						},
						success: function(data,success) {console.log(data); },
						error : function(jqXHR, textStatus, errorThrown) {
								if (jqXHR.status == 404) {
									alert("not found.");
								} else {
									alert("Error: " + textStatus + ": " + errorThrown);
								}
							}
					});

		 $.ajax({ 
			  url: "API_URL",
			  type: "GET",			  
		   }); 
		
	  
    
});

Note: according to latest Jquery documentation it is not recommended to use $.ajaxSetup();

Leave a Reply

Your email address will not be published. Required fields are marked *