伙计我在c#中有一个web服务,在javascript ajax中调用,值返回到javascript .Values作为列表从webservice返回到javascript。但是当我显示值时,它显示每个值未定义。成功调用Web服务我使用调试器检查了它。
我的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;
}
}
我调用webservice和显示值的代码是这样的:
<%@ 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 :(得分: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");
}
});
}