Why is the unlock method not executed?
Recently, we intend to lock the object on the editing page of the object, that is, when the object is modified by a user, the object cannot be modified at other times, this ensures that the same object is not modified by two people at the same time, so that the user data submitted first is overwritten by the user data submitted later.
The implementation scheme is to useJavaCodeDetermine whether the object is locked by another user. if the object is locked by another user, a prompt is displayed. if the object is not locked, the user enters the editing page of the object. When the user triggers"Window. onUnload"Method, callAjaxRequest to go to the server to unlock the object. Of course, we also considerAjaxThe request may fail, which will cause the object to be permanently locked. To solve this problem, when we lock an object, the maximum lock time cannot exceed20To some extent, this can avoid permanent object locking due to errors in some situations.
In the ie browser, the execution is normal, but on Firefox, chrome , but it will not notify the server to unlock the object. The error is found during the execution of the function window. onUnload . The code of the " window.
onUnload " function is as follows:
Function unlockobj (_ nobjid, _ nobjtype) {$. ajax ({type: "Post", URL :".. /include/object_unlock.jsp ", async: false, // set the synchronous request, no problem; for asynchronous requests, there will be a problem data: {objid: _ nobjid, objtype: _ nobjtype }, success: function (data) {return ;}, error: function (data) {return ;}); return ;}
In any browser, when the browser is closed, Window. onUnload All functions are executed. We know that the browser is executing Ajax , Ajax Object return status value ( Readystate ) Will change 1-> 2-> 3-> 4 , When Ajax The status value is 4 , That is Ajax Request execution completed ( Complete ). For Window.
OnUnload "Function, in IE In the browser, regardless Ajax The returned status value is Ajax Requested page (that is Object_unlock.jsp ). System. Out All output statements have values for output. But in Firefox, Chrome In other browsers Httpwatch , You can see Ajax The request is normal, Ajax Requested page (that is Object_unlock.jsp ). Sytem. Out No output of the output statement. After my test, only Ajax The returned status code of the request is 4 HourAjax Requested page (that is Object_unlock.jsp ). Sytem. Out Only output statements can be output.
I didn't understand why. This article tests the sendingAjaxThe request is to useJquery. jsClass Library (1.4.4Version) sent by the component.
the solution is to trigger the" window. onUnload "function, set the Ajax request to synchronous, that is, set the attribute " async " is " False ", see the red area of the code above.