问题
I use a webmethod to identify whether the user can "Delete a record".
Here is the JavaScript code before adding this access control.
$(".apply-delete-msg").live('click', function() {
return confirm("Are you sure you want to delete this item(s)?);
});
Now it will call the webMethod to validate the access
<WebMethod(EnableSession:=True)> _
Public Function CanAccess() As Boolean
Return ServerCode.IsAccessable
End Function
The new JavaScript then:
$(".delete-msg").live('click', function() {
MPWebService.CanAccess(
//B
function(boolRes) {
if (boolRes == true){
return confirm("Are you sure you want to delete this item(s)?");}
else{
alter("can't access");
return false;
}
});
// **Here is Comment A**: Return true/false
});
I want the ".Live" method to return a true/false if the user can access and confirm delete/cannot access or cancel the delete.
But if i'm right the method will call CanAccess first, and then Comment A:, finally Comment B, i.e. function(boolRes).
Because the value of boolRes is inside function(boolRes) and it is processed at last, it's tricky for me to get a return value from this method at Comment A position.
Any suggestions?
回答1:
The Web Service call is asynchronous and will return a result after you have returned from the click message. Therefore you always need to return false from the click method and re-design your page, so that the action that would have normally taken place after client side click (and true returned) to be performed after web service method returns. It would also be good to have a "Please wait..." message, while waiting for the call to complete.
回答2:
There is two ways to do it :
- do the comment A code into the boolRes==true block
- create a callback which contains the code of comment A and call this callback in the boolRes==true block
来源:https://stackoverflow.com/questions/9498576/trigger-prevent-page-event-by-using-asynchronous-webmethod-return-value-in-javas