Home > front end >  The ajax request is sent
The ajax request is sent

Time:12-04

Because the company business needs, need in the current user closes the browser page to update the user to modify data,
I use onbeforeunload, on two pages using the same monitoring events, but only one page can play a role, the page doesn't work can enter the ajax method, but the request is sent less than the background, no problem, and the path parameters by using other method calls is normal, can change a browser to open this page sends a request to the background, and the great spirit show

//close the current page to save the modified data
Window. Onbeforeunload=onbeforeunload_handler;
//the window. The onunload=onunload_handler;
The function onbeforeunload_handler () {
The debugger.
Cui (" # deviceList - table "). The submit ();
}

//update method
Window. SubmitTechData=https://bbs.csdn.net/topics/function (editableGridObj changeData) {
The debugger.
Var allDate={};
//update the data
If (changeData. The updateData & amp; & ChangeData. The updateData. Length & gt; 0 {
AllDate=changeData. The updateData;
}
Cui. HandleMask. The show ();
//data operations
If (allDate & amp; & AllDate!=' ') {
The jQuery ajax ({
Url: "# url ()/... "
Type: "POST",
Data: JSON. Stringify (allDate),
DataType: "json",
Async: false,
ContentType: "application/json,"
Success: the function (data) {
Cui. HandleMask. Hide ();
If (data & gt; 0 {
Cui. Message (" successful ", "success");
} else {
Cui. Message (" fail ", "error");
}
EditableGridObj. SubmitComplete ();
}
});
};
}

CodePudding user response:

If you write a single page application

With a window. The addEventListener (" beforeunload ", myScript); This method is to add to monitor,

Your window is used in two places. Onbeforeunload=myScript written as, the window is a global object, different routing is also in the same window,

Window. The onbeforeunload is a variable can only be assigned a value, add behind will naturally give cover, the front of the

The window. The addEventListener (" beforeunload ", myScript); This method can add multiple same listening at the same time, there will be no coverage problems

And add the listening remember listening to get rid of the window. The removeEventListener (" beforeunload ", myScript);

CodePudding user response:

Need the ajax callback function in onbeforeunload with return to return to an empty string
  •  Tags:  
  • Ajax
  • Related