sea*_*eal 15 javascript ajax jquery curl
我正在尝试将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
Run Code Online (Sandbox Code Playgroud)
有了这个,我想将命令转换为$ .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) {
//
}
Run Code Online (Sandbox Code Playgroud)
Ahm*_*Ali 19
默认情况下$.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");
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30977 次 |
| 最近记录: |