3
votes

I have asp.net web site in c#.

On Dropdownlist Onchange() event i'm calling this jquery function, which throws:

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)

1
You should look at the logs on the server side - presumably an exception is being thrown in `GetRef, but we can't tell what that is just from looking at your code.Jon Skeet
"which throws: function error(){[native code]}". No, it doesn't throw any errors. You are just alerting a native constructor. String representation of the Error constructor is function error(){[native code]}. Use console.log for debugging and check the network tab of the browser developer tools to check the server response.undefined
may be you need to mark your web method as static like public static string GetRef(int id)Amit Soni

1 Answers

1
votes

My first suggestion would be to make the method attributed as [WebMethod] to static.

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

If that didn't work out, try to check whether your ajax url is pointing to the method correctly.

url: 'WT.aspx/GetRef',

and also check whether you are passing 'this' as the function parameter for GetDescription(a).

 <select onchange="GetDescription(this)">
    <option value="1">text1</option>
    <option value="2">text2</option>
    <option value="3">text3</option>
 </select>