如何从javascript调用Web服务
问题描述:
我正在尝试从javascript调用我的.asmx webservice,但是成功函数没有执行
这里是我的javascript
I'm trying to call my .asmx webservice from javascript but the success function is not executing
here is my javascript
<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在我的网络服务上设置了一个断点,我的网络服务返回的数据很好
这里是我的webservice
i put a break point on my web service and my web service returns the data good
here is my webservice
[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;
}
}
}
答
请参阅以下链接
致电在常规时间间隔内使用JavaScript的ASP.NET Web服务 [ ^ ]
http://pavanarya.wordpress.com/2012/05/20/calling-a-web-service-from-javascript-using-xmlhttprequest/ [ ^ ]
谢谢,
-RG
See the below links
Calling ASP.NET Webservice using JavaScript on Regular Interval of Time[^]
http://pavanarya.wordpress.com/2012/05/20/calling-a-web-service-from-javascript-using-xmlhttprequest/[^]
Thank You,
-RG