Dep*_*ify 12 ajax jquery post xmlhttprequest
我编写了一个使用$.postJQuery调用大量调用的Web应用程序.现在我想发送withCredentials: true它以保持会话活着,看起来像这样$.ajax(并且也像这样):
$.ajax({
type: 'post',
url: 'http://example.com/server/api.php',
crossDomain: true,
dataType: "json",
xhrFields: {
withCredentials: true
},
data: {
username : 'test',
password : 'test'
},
success: function (d) {
$('body').html(d.status);
}
});
Run Code Online (Sandbox Code Playgroud)
这是因为我现在想将PHP文件上传到我的服务器并使用Cordova导出客户端.(withCredentials: true仅包括在我的本地主机服务器上进行测试)我可以将其打包到$.post呼叫中还是需要更换所有呼叫?(我会写一个类似于$ .post的新函数)
Spo*_*key 18
您可以使用jQuery.ajaxSetup()来设置每个ajax请求将使用的默认选项(包括$.post和$.get)
$.ajaxSetup({
crossDomain: true,
xhrFields: {
withCredentials: true
},
username: 'test',
password: 'test'
});
$.post('http://example.com/server/api.php', {
username: 'test',
password: 'test'
}, function (d) {
$('body').html(d.status);
}, 'json');
Run Code Online (Sandbox Code Playgroud)
还有关于此API的警告
注意:此处指定的设置将影响对$ .ajax或基于Ajax的派生类(如$ .get())的所有调用.这可能导致不良行为,因为其他呼叫者(例如,插件)可能期望正常的默认设置.出于这个原因,我们强烈建议不要使用此API.相反,在调用中显式设置选项或定义一个简单的插件来执行此操作.
来自jQuery文档
| 归档时间: |
|
| 查看次数: |
27575 次 |
| 最近记录: |