Search code examples
jqueryasp.netinternet-explorergoogle-chromepagemethods

Javascript call to ASP.NET Page Method working on IE but not on Chrome / Firefox


I have the following code in an ASPX page that, when a PayPal icon is clicked, connects with a Page Method and retrieves the PayPal token of a session. When this is executed in IE, it works perfectly, but when it is executed in Chrome or Firefox, the $.ajax call of jQuery returns immediately an error of code 0.

$(document).ready(function () {
            $("input.PayPal").click(function () {
                $.ajax({
                    type: "POST",
                    url: "MyCurrentPage.aspx/PayPal",
                    data: "{IdRecibo : " + $(this).attr("data-value") + "}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (result) {
                        if (result.d.Result == "Success") {
                            window.location.href = result.d.Redirect;
                        }
                        else
                            alert("El servicio de PayPal está temporalmente deshabilitado.");
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });
            });

Solution

  • A slight change of code solves the problem with Chrome. It looks like the JSON response of the ASP.NET Page Method is not correctly parsed if return false; is not the last part of the $.ajax call.

    This is the working code:

        $("input.PayPal").click(function () {
            $.ajax({
                type: "POST",
                url: '<%= Page.Request.Url.AbsolutePath + "/PayPal" %>',
                data: JSON.stringify({ IdRecibo: $(this).attr("data-value") }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: "false",
                timeout: 10000,
                success: function (result) {
                    if (result.d.Result == "Success") {
                        window.location.href = result.d.Redirect;
                    }
                    else
                        alert("El servicio de PayPal está temporalmente deshabilitado.");
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
            return false;
        });