javascript - 函数错误(){ [ native 代码]}

标签 javascript c# jquery asp.net

我有一个用 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/

相关文章:

c# - 如何从 C# 调试 session 中调试存储过程?

javascript - Knockout JS - 无容器控制使用模板中断 Foreach

javascript - 有没有办法覆盖 setTimeout 函数,所以它使用微秒而不是毫秒

javascript - JW Player 6 插件在 Firefox 中不工作 - Drupal

php - 从Facebook组获取YouTube嵌入

javascript - ajax调用完成后做一些事情

jquery - 引导模式关闭后,html5 视频继续在后台播放

javascript - 如何使用jquery选择输入

c# - Ultrawingrid - 根据唯一值选择行

c# - 如何访问 WCF 中继承的对象属性?