asp.net pagemethod调用,使用jquery,返回错误状态500
我一直没有找到任何解决此问题的好方法,所以我需要som帮助。asp.net pagemethod调用,使用jquery,返回错误状态500
我有一个PageMethod,我需要调用jQuery。客户端代码如下所示:
<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。我看不到这样做的原因。
请检查你在这里得到: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)。
我试过提醒var selectedvalue = $(this).val();,它会写入我在DDL中选择的数字。即使我注释掉“Product _product = Product.GetProductByDate(DateTime.Today); ”并使用硬编码数字,我也会收到错误消息。我现在试着写alert(errorThrown);它给了我一个[object XMLHttpRequest]错误。我是否正确地将参数传递给PageMethod?你在测试中如何称呼该方法? – Soeren 2011-03-16 12:18:48
@Soeren我只是在下拉菜单中更改了vlaue,请尝试在Web方法中放置断点并对其进行调试。 – TheVillageIdiot 2011-03-16 12:49:04
您是否能够在调试器中运行您的webmethod以确保它正在调用并返回值? – Geoff 2011-03-16 10:58:27
你有试过调试吗?也许传给'antal'的值不是整数。 – TheVillageIdiot 2011-03-16 10:59:52