0
是否可以使用jQuery的$ .post函数并在浏览器中设置cookie?我正在设置它,以便我的用户可以通过发送POST变量自动登录到某个站点。
是否可以使用jQuery的$ .post函数并在浏览器中设置cookie?我正在设置它,以便我的用户可以通过发送POST变量自动登录到某个站点。
要回答你的问题,是的,你可以做一个ajax调用并让服务器设置必要的会话/认证cookie。
您也可以通过$ .ajax进行POST,这可能会给您多一点(但可能不需要)控制事件。例如:
$.ajax({
async: false,
cache: false,
type: 'post',
dataType: 'json', // json...just for example sake
data: ({
'username': $('#Username').val(),
'password': $('#Password').val()
}),
url: '/Login/Authorize.php',
success: function (data) {
// retrieve a success/failure code from the server
if (data === '1') { // server returns a "1" for success
// success!
// do whatever you need to do
} else {
// fail!
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// something went wrong with the request
alert(XMLHttpRequest.responseText);
}
});
是的,您可以将$.post
添加到服务器端页面,该页面处理设置cookie和所有业务的会话。