Converting curl cmd to jQuery $.ajax() Converting curl cmd to jQuery $.ajax() curl curl

Converting curl cmd to jQuery $.ajax()


By default $.ajax() will convert data to a query string, if not already a string, since data here is an object, change the data to a string and then set processData: false, so that it is not converted to query string.

$.ajax({    url: "http://www.example.com/api",    beforeSend: function(xhr) {       xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));     },    type: 'POST',    dataType: 'json',    contentType: 'application/json',    processData: false,    data: '{"foo":"bar"}',    success: function (data) {      alert(JSON.stringify(data));    },    error: function(){      alert("Cannot get data");    }});