Search code examples
jqueryasp.netpagemethods

asp.net pagemethod call, using jquery, returns error status 500


I haven't been able to find any good solutions to this issue, so I need som help.

I have a PageMethod that I need to call with jquery. The client code looks like this:

<script type="text/javascript">
    $(function () {
        $("#MainContent_ddl_antal").change(function () {
            var selectedvalue = $(this).val();
            //alert(selectedvalue);
            $.ajax({
                type: "POST",
                url: "betaling.aspx/GetTotPrice",
                data: "{'antal':" + selectedvalue + "}",

                //data: JSON.stringify({ antal: selectedvalue }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Set label text to method's return.
                    alert(msg.d);
                    $('#<%= lbl_totpris.ClientID %>').html(msg.d);
                },
                error: function (error) {
                    alert(error.status);
                }
            });

        });
    });

</script>

and the codebehind PageMethod looks like this:

[WebMethod]
public static double GetTotPrice(int antal)
{
    double totpris = 0;
    Product _product = Product.GetProductByDate(DateTime.Today);

    if (_product != null)
    {
        totpris = _product.ProductNuPris * antal;
    }
    return totpris;
}

The call returns an error 500. I can't see the reason for this.


Solution

  • Please check what you are getting here: var selectedvalue = $(this).val(); I think this is not returning integer value. I have tried your code with following list and it works fine:

    <select id="testList">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
    </select>
    
    [WebMethod]
    public static double GetTotPrice(int antal)
    {
        return 5.0 * antal;
    }
    

    If the value from select list is correct then this line may be causing problem:

    Product _product = Product.GetProductByDate(DateTime.Today);
    

    Please try debuging it once using Visual Studio and FireBug (Firefox).