Web服务返回给javascript的值是未定义的

Values returned to javascript by webservice come as undefined

本文关键字:未定义 javascript 服务 返回 Web      更新时间:2023-09-26

伙计们,我有一个 c# 中的 Web 服务,它在 javascript ajax 中调用,值返回到 javascript。值作为列表从 Web 服务返回到 javascript。但是当我显示值时,它显示每个值都未定义。Web服务被成功调用,我使用调试器检查了它。

我的网络服务是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for MyService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{
    public MyService()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    public List<myclass> dataRecieve(int roll, string name)
    {
        List<myclass> list = new List<myclass>();
        myclass mc = new myclass(9082, "Mubashir", "MCA1");
        list.Add(mc);
        mc = new myclass(9093, "Golu", "MCA1");
        list.Add(mc);
        mc = new myclass(872, "Fida", "MCA1");
        list.Add(mc);
        return list;
    }
}
public class myclass
{
    int roll;
    string name, address;
    public myclass(int r, string n, string a)
    {
        name = n;
        roll = r;
        address = a;
    }


}

我调用 Web 服务和显示值的代码如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function ShowProgress() {
        setTimeout(function () {
            var modal = $('<div />');
            modal.addClass("modal");
            $('body').append(modal);
            var loading = $(".loading");
            loading.show();
            var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
            var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
            loading.css({ top: top, left: left });
        }, 200);
    }
    $('form').live("submit", function () {
        ShowProgress();
    });
</script>
   <%-- 
        <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "index.aspx/GetAutoCompleteData",
                        data: "{'Name':'" + document.getElementById('txtSearch').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }
    </script>

       --%>
    <script type ="text/javascript" >
        $(document).ready(function(){
            $("#btni").click(function () {
                var dataSent="{roll:"+1+",name:'""+"Mubashir'"}"
                $.ajax({
                    type: "Post",
                    url: "MyService.asmx/dataRecieve",
                    data: dataSent ,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        var studentInfo = response.d;
                        $("#output").empty();
                        $.each(studentInfo, function (index, Info) {
                            //alert(index);
                            //alert(index.Info);
                            $("#output").append(
                                '<strong>Roll : </strong>' + Info.roll + ' ' +
                                 '<strong>Name : </strong>' + Info.name + ' ' +
                                  '<strong>Address : </strong>' + Info.address + '<br/> '
                                );
                        });

                    },
                    failure: function (msg) {

                        $("#output").text(msg);
                    }


                });

            });

});
    </script>
<style type="text/css">
    .modal
    {
        position: fixed;
        top: 0;
        left: 0;
        background-color: black;
        z-index: 99;
        opacity: 0.8;
        filter: alpha(opacity=80);
        -moz-opacity: 0.8;
        min-height: 100%;
        width: 100%;
    }
    .loading
    {
        font-family: Arial;
        font-size: 10pt;
        border: 1px solid blue ;
        width: 200px;
        height: 100px;
        display: none;
        position: fixed;
        background-color: White;
        z-index: 999;
        border-radius :6px;
    }
</style>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .ErrorControl
        {
            background-color: #FBE3E4;
            border: solid 1px Red;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
  <button id="btni" type ="button"  >Get Students</button>
       <div id="output"></div>
  </form> 
</body>
</html>

尝试在运行自动完成之前获取数据:

    function SearchText() {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "index.aspx/GetAutoCompleteData",
            data: "{'Name':'" + document.getElementById('txtSearch').value + "'}",
            dataType: "json",
            success: function (data) {
                $(".autosuggest").autocomplete({
                    source: data
                });
            },
            error: function (result) {
                alert("Error");
            }
        });
    }