简体   繁体   English

使用Ajax请求检查会话

[英]Check session using Ajax request

Im checking if a session is valid using Ajax request. 我使用Ajax请求检查会话是否有效。

Im using the below code to accomplish it... 我用下面的代码来完成它...

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 = './';

            }
        }

It all works fine...The only problem is when I have a FORM submission... 一切正常...唯一的问题是当我提交表单时...

 <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>

When I submit the above form I check if the session is valid using the below code - 当我提交上述表格时,我使用以下代码检查会话是否有效-

 $('input[id=search_string]').on('keyup', function(e) {
    if (e.which == 13) {
        alert('called_for')
        called_for = "ENTER"
        checksession()
   }
   });

When I do this the POST request to run the CheckSession is cancelled. 当我这样做时,运行CheckSession的POST请求被取消。

Error from Chrome internals - 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

Form submission: 表格提交:

 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!");
   }

   }

It gets cancelled because the form is submitted without waiting for the session check. 它被取消,因为提交表单时无需等待会话检查。 If you want to test the form before submit it, you should use the on submit event and return false. 如果要在提交表单之前测试表单,则应使用on Submit事件并返回false。

But why do you check the session before submit ? 但是,为什么要在提交之前检查会话? The search script should test the session itself. 搜索脚本应测试会话本身。

You set a global variable called_from. 您设置一个名为_from的全局变量。 This is not good practice. 这不是一个好习惯。 Rather give your session check function the callback function as parameter. 而是为您的会话检查函数提供回调函数作为参数。

function checksession(callback) {
    ...
            if (session_ind == "Y"){
                alert_message = "Y"
                callback()
   ...
}

Then call it like this : 然后这样称呼它:

if (e.which == 13) {
    alert('called_for')
    called_for = "ENTER"
    checksession(formSubmit1())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM