The reason the call to your web method is failing is because
your are not canceling the postback of your LinkButton
.
You have to return false from the OnClientClick event to cancel
the postback. The code below should fix it:
function doStuff() {
var a = 'a';
var b = 'b';
PageMethods.doStuffWebMethod(a, b, doStuffSuccess, doStuffFail);
return false; // Cancel postback.
}
function doStuffSuccess() {
alert('Success!');
}
function doStuffFail() {
alert('Failure!');
}
<asp:LinkButton ID="mybutton" runat="server" CausesValidation="false" OnClientClick="return doStuff();">Do stuff!</asp:LinkButton>
For a more sophisticated solution for canceling the default behaviour of a browser (postback) please have a look on the following stackoverflow question and answer.
The reason you are getting those different results for the different browsers is maybe due to different implementations of the browser vendors. But I am not sure about this.
You can also verify this behaviour by creating a network protocol trace (press F12 in Google Chrome browser and switch to the network tab).
The protocol in case you do not return false from the doStuff() method:
- The page method
doStuffWebMethod
is called. Then you get the JavaScript message box. (HTTP POST) - Your WebForm.aspx is requested. (HTTP POST)
- Then WebResource.axd and ScriptResource.axd is requested. (HTTP GET)
Number 2. and 3. shows that a postback is executed after the request of your page method.
The protocol in case you do return false from the doStuff() method:
- Only the page method
doStuffWebMethod
is called. (HTTP POST)
The second trace clearly shows that there is no postback executed.
If you want the postback to happen for your LinkButton then you could manually trigger the postback in the JavaScript success handler using the __doPostBack() method after the page method comes back:
function doStuffSuccess() {
alert('Success!');
__doPostBack('<%= mybutton.UniqueID %>', ''); // trigger the postback.
}