我使用Ajax请求检查会话是否有效。
我使用以下代码来完成它......
function checksession() {
$.ajax({
type:'POST'
,url:'CheckSession'
,success: validateresult
,data: { antiCSRF : '{{acsrf}}',
session_id: '{{session_id}}'}
,error: function(){ alert('Session check failed') }
})
function validateresult(session_ind){
alert('called_form validateresult = ' + called_for)
if (session_ind == "Y"){
alert_message = "Y"
if (called_for == "SEARCH") {formSubmit()}
else if (called_for == "ENTER" ) {formSubmit();}
else if (called_for == 'DATA') { $('#datasetform').submit();}
else if (called_for == 'FEEDBACK') { createfeedback()}
else if (called_for == 'HELP') {createhelp()}
}else{
if (alert_message == "Y") {
alert("Your session has been timed-out ");
}
alert_message = "Y";
window.location = './';
}
}
一切正常......唯一的问题是我提交了一份表格提交......
<form id="bigsearchform_new" method="post" action="Paid">
<label style="display:none" for="search_string1">SEARCH</label>
<input id="search_string" name="search_string1" type="text" class="startnewsearch rounded" placeholder="Search..." maxlength="500" >
<input id="searchButton1" type="button" class="searchButton" title="Click here to search the database">
<input type="hidden" name="antiCSRF" value="{{acsrf}}" />
<input type="hidden" name="session_id" value="{{session_id}}" />
<input type="hidden" name="commodity_id" id="commodity_id" />
</form>
当我提交上述表格时,我会使用以下代码检查会话是否有效 -
$('input[id=search_string]').on('keyup', function(e) {
if (e.which == 13) {
alert('called_for')
called_for = "ENTER"
checksession()
}
});
执行此操作时,将取消运行CheckSession的POST请求。
Chrome内部错误 -
t=254662 [st=60] HTTP_TRANSACTION_READ_RESPONSE_HEADERS
--> HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Mon, 11 Aug 2014 19:31:36 GMT
Server: Apache
X-Frame-Options: DENY
Content-Length: 1
Connection: keep-alive
表格提交:
function formSubmit()
{
alert('calling main page = ' + $("#search_string").val().length )
if ($("#search_string").val().length != 0) {
$("#bigsearchform_new").submit();
} else {
alert("Please enter a search term!");
}
}
答案 0 :(得分:0)
它被取消,因为提交表单时没有等待会话检查。如果要在提交表单之前测试表单,则应使用on submit事件并返回false。
但为什么在提交之前检查会话?搜索脚本应该测试会话本身。
您设置了一个名为_from的全局变量。这不是好习惯。而是将会话检查功能作为参数提供回调函数。
function checksession(callback) {
...
if (session_ind == "Y"){
alert_message = "Y"
callback()
...
}
然后这样称呼:
if (e.which == 13) {
alert('called_for')
called_for = "ENTER"
checksession(formSubmit1())
}