asp.net pagemethod调用,使用jquery,返回错误状态500

时间:2011-03-16 10:54:12

标签: jquery asp.net pagemethods

我无法找到任何解决这个问题的好方法,所以我需要帮助。

我有一个需要用jquery调用的PageMethod。客户端代码如下所示:

<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>

并且代码隐藏的PageMethod看起来像这样:

[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;
}

调用返回错误500.我看不出原因。

1 个答案:

答案 0 :(得分:0)

请检查你在这里得到的信息:var selectedvalue = $(this).val();我认为这不会返回整数值。我已尝试使用以下列表的代码,它工作正常:

<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;
}

如果选择列表中的值正确,则此行可能会导致问题:

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

请尝试使用Visual Studio和FireBug(Firefox)对其进行一次调试。