将命令curl转换为javascript

我正在尝试将curl中的命令转换为javascript。 我在谷歌搜索过,但我找不到可以帮助我的解决方案或解释。 命令curl是这样的:

curl https://www.google.com/accounts/ClientLogin --data-urlencode Email=mail@example.com --data-urlencode Passwd=******* -d accountType=GOOGLE -d source=Google-cURL-Example -d service=lh2 

有了这个我想将命令转换为$ .ajax()函数。 我的问题是,我不知道我必须在函数setHeader中放置命令curl中的选项。

 $.ajax({ url: "https://www.google.com/accounts/ClientLogin", type: "GET", success: function(data) { alert('hello!' + data); }, error: function(html) { alert(html); }, beforeSend: setHeader }); function setHeader(xhr) { // } 

默认情况下, $.ajax()会将数据转换为查询字符串(如果还不是字符串),因为这里的数据是对象,将数据更改为字符串然后设置processData: false ,以便它不会转换为查询字符串。

 $.ajax({ url: "https://www.google.com/accounts/ClientLogin", 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"); } });