2015-01-06 4 views
0

Ребята У меня есть веб-сервис в C#, который вызывается в javascript ajax, а значения возвращаются в javascript. Ценны возвращаются как список из webservice в javascript. Но когда я показываю значения, это показывает мне undefined для каждого значения. Webservice называется успешно, я проверил, что с помощью отладчика.Значения, возвращаемые javascript от webservice, не определены

Мой Webservice как это:

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; 

    } 





} 

Мой код для вызова веб-сервиса и отображения значения, как это:

<%@ 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> 

ответ

0

Попробуйте получить ваши данные перед запуском автозаполнения:

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"); 
      } 
     }); 

    } 
Смежные вопросы