Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to call my .asmx webservice from javascript but the success function is not executing
here is my javascript
C#
<script type="text/javascript" >
      function getResult()
      {
          var info = document.getElementById("InputText").value;
          HTML5.BusTracking.GetBySpot(info,SucceededCallback);
          alert(info);
      }
      function SucceededCallback(result)
      {
          alert("yes");
          document.getElementById("ResultText").html = result["Latitude"];
      }
      function FailedCallback()
      {
          alert(error.message());
      }
  </script>





i put a break point on my web service and my web service returns the data good
here is my webservice
C#
[System.Web.Script.Services.ScriptService]
public class BusTracking : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public DataTable GetBySpot(string spot)
    {
        string conn = "Data Source=svhqdev01;Initial Catalog=Bustracking;Persist Security Info=True;User ID=bustrack;Password=abcd1234;MultipleActiveResultSets=True;Application Name=EntityFramework";
        DataTable table = new DataTable();
        table.TableName = "Result";

        using(SqlConnection connection=new SqlConnection(conn))
        {
            SqlCommand cmd = new SqlCommand("GetByspot",connection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter = new SqlParameter("@spot", spot);
            cmd.Parameters.Add(parameter);
            connection.Open();
            SqlDataAdapter adapter=new SqlDataAdapter(cmd);
            SqlDataReader reader = cmd.ExecuteReader();
            table.Load(reader);
            return table;
        }
    }
}
Posted
Updated 22-Jul-14 20:50pm
v2
Comments
Kornfeld Eliyahu Peter 23-Jul-14 2:58am    
Call web service from plain JavaScript can be a long and problematic process...I suggest you use jQuery.ajax...
breab 23-Jul-14 3:01am    
I've also tied that but the web service is not executing



<script type="text/javascript">
var url = "http:http://localhost:2364/BusTracking.asmx";

$.ajax({
type: "POST",
url: url + "?op=GetBySpot",
data: "{spot:'2'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: SucceededCallback,
error: FailedCallback
});

}
function SucceededCallback(result) {
alert("yes");
document.getElementById("ResultText").html = result["Latitude"];
}

function FailedCallback(error) {
alert(error.message());
</script>
Kornfeld Eliyahu Peter 23-Jul-14 3:03am    
What does it means 'not executing'?
breab 23-Jul-14 3:06am    
I put a break point on the web service function

when i use javascript the it enterrs to the break point but when i use jquery ajax
it doesn't enter to the break point
Kornfeld Eliyahu Peter 23-Jul-14 3:14am    
In most cases that means that your URL and/or data are not fir the declarations of web service, so there is no map between your call and the server and the call goes out without response...
What I can see that in the JQuery solution you use BusTracking.asmx but from the code you provided BusTracking is a class...
In anyway the URL should look like whatever.asmx/functiontocall, and the ?op=GetBySpot seems to me wrong...

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900