我有一个用 c# 编写的 asp.net 网站。
在 Dropdownlist Onchange()
事件中,我调用了这个 jquery 函数,它抛出:
function error(){[native code]}
<script type="text/javascript">
function GetDescription(a) {
alert(a); // the dropdown item selected value
var id = (!isNaN($(a).val())) ? parseInt($(a).val()) : 0;
$.ajax({
type: 'POST',
contentType: "application/json; charset-8;",
url: 'WT.aspx/GetRef',
data: "{ 'id':'" + id + "'}",
success: function (data) {
alert(data);
},
error: function (data) {
alert(Error);
}
});
}
</script>
WT.aspx/GetRef
[WebMethod] public string GetRef(int id) { DataTable dt = new DataTable(); SqlParameter[] p = new SqlParameter[1]; p[0] = new SqlParameter("@RefID", id); dt = dl.GetDataWithParameters("Sp_WT_GetRef", p); string data = dt.Rows[0]["Description"].ToString() +"|"+ dt.Rows[0]["PriceInUSD"].ToString(); return data; }
http://localhost:54576/resources/demos/style.css Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:54576/AutomobileWebApp/WT.aspx/GetRef Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:54576/resources/demos/style.css Failed to load resource: the server responded with a status of 404 (Not Found)
最佳答案
我的第一个建议是将属性为 [WebMethod] 的方法设为静态。
[WebMethod]
public static string GetRef(int id)
{
DataTable dt = new DataTable();
SqlParameter[] p = new SqlParameter[1];
p[0] = new SqlParameter("@RefID", id);
dt = dl.GetDataWithParameters("Sp_WT_GetRef", p);
string data = dt.Rows[0]["Description"].ToString() +"|"+ dt.Rows[0]["PriceInUSD"].ToString();
return data;
}
如果没有成功,请尝试检查您的 ajax url 是否正确指向该方法。
url: 'WT.aspx/GetRef',
并检查您是否将“this”作为 GetDescription(a) 的函数参数传递。
<select onchange="GetDescription(this)">
<option value="1">text1</option>
<option value="2">text2</option>
<option value="3">text3</option>
</select>
关于javascript - 函数错误(){ [ native 代码]},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30230182/