seal seal - 1 year ago 389
jQuery Question

Convert command curl to javascript

I'm trying to convert a command in curl to javascript. I have searched in google but I don't find a solution or a explanation that can help me. The command curl is this:

--data-urlencode Passwd=*******
-d accountType=GOOGLE
-d source=Google-cURL-Example
-d service=lh2

With this I want convert the command to $.ajax() function. My problem is that I don´t know what I have to put in function setHeader for the options present in command curl.

url: "",
type: "GET",

success: function(data) { alert('hello!' + data); },
error: function(html) { alert(html); },
beforeSend: setHeader

function setHeader(xhr) {


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.

 url: "",
 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) {
  error: function(){
   alert("Cannot get data");