使用javascript调用网络服务 [英] Call Webservice using javascript

查看:52
本文介绍了使用javascript调用网络服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Web服务,名称为" WebService ".
该Web服务具有一个称为" GetStudentdata "的网络方法.
这是网络方法代码:

I have created one webservice which name is " WebService ".
This webservice has one webmethod called "GetStudentdata".
here is the webmethod code:

[WebMethod]
    public List<student> GetStudentdata()
    {
        student objstudent;

        string str = ConfigurationManager.ConnectionStrings["connection_String"].ConnectionString;
        SqlConnection conn = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("sp_getStudentData", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        ///SqlDataAdapter to populate our DataSet
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = cmd;
        ///DataSet to hold the users information      
        DataTable dsInfo = new DataTable();

        try
        {

            adapter.Fill(dsInfo);

            //return the DataSet to the calling aspx page
            List<student> drlist = new List<student>();

            foreach (DataRow row in dsInfo.Rows)
            {
                objstudent = new student();
                objstudent.rollnumber = Convert.ToInt32(row["Rollno"]);
                objstudent.studentname = row["Studentname"].ToString();
                objstudent.subject = row["Subject"].ToString();

                drlist.Add(objstudent);
            }

            return drlist;
        }

        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write(ex.Message);
            return null;
        }
        finally
        {
        }

    }
    public class student
    {
        public int rollnumber;
        public string studentname;
        public string subject;
    }



现在,我已将此Webservice调用到我的.aspx页面中
这是.aspx页面代码.



Now, I have called this webservice in to my .aspx page
here is the .aspx page code.

<html xmlns="http://www.w3.org/1999/xhtml">
   <head  runat="server">
      <title></title>
      <script src="jquery-1.7.1.min.js" type="text/javascript"></script>
      <script type="text/javascript">
         function call() {
            $.ajax({
               type: "GET",
               url: "http://localhost:2156/Websitetesting/WebService.asmx/GetStudentdata",
               dataType: document.json,
               error: function (xhr, ajaxOptions, thrownError) { alert('error:' + xhr.status); },
               success: function (xmlDoc) {
                  var abc;
                  $(xmlDoc).find("ArrayOfStudent").each(function () 
                  {
                     $(this).find("student").each(function () {
                     abc.value = abc + $(this).find('rollnumber').text();
                     abc.value = abc + $(this).find('studentname').text();
                     abc.value = abc + $(this).find('subject').text();
                     alert(abc.value);
                  });
               });
            }
         });
      }
      </script>
   </head>
   <body>
      <form id="form1"  runat="server">
         <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="call();return false;" />
      </form>
   </body>
</html>



我的网络服务将这样的数据返回给我.



My webservice return me the data like this.

<arrayofstudent>
   <student>
      <rollnumber>1</rollnumber>
      <studentname>ABC</studentname>
      <subject>Maths</subject>
   </student>
   <student>
      <rollnumber>2</rollnumber>
      <studentname>Xyz</studentname>
      <subject>Computer</subject>
   </student>
   <student>
      <rollnumber>3</rollnumber>
      <studentname>Pqr</studentname>
      <subject>Science</subject>
   </student>
</arrayofstudent>



我想使用javascript将这些数据显示到datagrid中,
但我做不到.
所以请任何人都可以帮助我.
在此先感谢.



I want to display this data in to datagrid using javascript,
but I am unable to do.
So please anyone can help me.
thanks in advance.

推荐答案

.ajax({ 类型:" , url:" , dataType:文档 .json, 错误:功能(xhr,ajaxOptions,throwError){alert(' 功能(xmlDoc){ var abc;
.ajax({ type: "GET", url: "http://localhost:2156/Websitetesting/WebService.asmx/GetStudentdata", dataType: document.json, error: function (xhr, ajaxOptions, thrownError) { alert('error:' + xhr.status); }, success: function (xmlDoc) { var abc;


(xmlDoc).find(" ArrayOfStudent").each(
(xmlDoc).find("ArrayOfStudent").each(function () {


( this ).find(" 学生").each(
(this).find("student").each(function () { abc.value = abc +


这篇关于使用javascript调用网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆