我在C#中有ASP.NET网站。
在下拉列表 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] 公共字符串getRef(int id) { DataTable DT = new Datatable(); sqlParameter [] p =新的sqlparameter [1]; p [0] = new sqlParameter("@refid",id); dt = dl.getDatawithParameters(" sp_wt_getref",p); 字符串data = dt.Rows [0] [" description"]。 返回数据; }
http://localhost:54576/resources/demos/style.css无法加载 资源:服务器以404的状态响应(找不到) http://localhost:54576/automobilewebapp/wt.aspx/getref无法加载 资源:服务器的响应为500(内部服务器 错误)http://localhost:54576/resources/demos/style.css无法加载 资源:服务器以404(未找到)状态响应
我的第一个建议是使该方法归因于[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',
,还检查您是否将"此"作为getDescription(a)的函数参数。
<select onchange="GetDescription(this)">
<option value="1">text1</option>
<option value="2">text2</option>
<option value="3">text3</option>
</select>