如何在asp.net中使用jquery和Json从WebService中使用WebMethods

时间:2014-04-24 14:57:18

标签: c# jquery asp.net json web-services

我正在开发一个asp.net Web应用程序,我想在html标签中使用jquery,json和webservices在Html页面中显示Customer City列表。这是我的Html页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="format-detection" content="telephone-no" />
<meta name="viewport" content="width=device-width, height=device-height, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
<title>News</title>
<link href="css/jquery.mobile.css" rel="stylesheet" />

<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.mobile.js" type="text/javascript"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script language="javascript" type="text/javascript">
    //function GetCompanies() {
    $(document).ready(function () {

        $.ajax({
            type: "POST",
            url: "JsonServices.asmx/BindDatatable",
            data: "{}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",

            success: OnSuccess,
            error: OnError
        });

        function OnSuccess(data) {
            //alert(JSON.stringify(data.d));
            //document.getElementById("lbljson").innerText = data.d;
            //document.getElementById("lbljson").innerText = 'hello';
            //alert('hello');

            //for (var i = 1; i < data.d.length; i++) {
            //    $("#olList").append("<li>" + data.d[i].CompanyName + "</li>");
            //    //alert('hello');
            //}

            $.each(data.d, function (key, value) {
                $("#NewsList").append("<li>" + value.City + "</li>");
            })

        }
        function OnError(data) {

        }
    });
</script>
<script>
    $(document).bind('mobileinit', function () {
        $.mobile.pushStateEnabled = false;
    });
</script>

</head>
<body>
<div id="home" data-role="page" data-theme="a">
    <div data-role="header">
        <h3>News</h3>
    </div>
    <div data-role="content">
        <ol  id="NewsList">

        </ol>
    </div>

</div>

</body>
</html>

这是My WebService,它位于我的asp.net应用程序的根目录中:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for JsonServices
/// </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.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class JsonServices : System.Web.Services.WebService {

public JsonServices () {

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
}

[WebMethod]
public string HelloWorld() {
    return "Hello World";
}
[WebMethod]
public static Customer[] BindDatatable()
{
    DataTable dt = new DataTable();
    List<Customer> details = new List<Customer>();

    using (SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress; Initial Catalog=Northwind; Integrated Security=True;"))
    {
        using (SqlCommand cmd = new SqlCommand("select TOP 10 * from Customers", con))
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach (DataRow dtrow in dt.Rows)
            {
                Customer user = new Customer();
                user.Address = dtrow["Address"].ToString();
                user.CompanyName = dtrow["CompanyName"].ToString();
                user.ContactName = dtrow["ContactName"].ToString();
                user.City = dtrow["City"].ToString();
                details.Add(user);
            }
        }
    }
    return details.ToArray();
}

public class Customer
{
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
}
}

但是WebService“JsonServices”中的WebMethod“BindDatatable”并没有返回任何数据,即使控件没有设置断点。但是,如果我在任何.aspx页面内写这个web方法然后它工作正常,但如果我在任何web服务中写这个webmethod它不会运行.. 请帮帮我。

0 个答案:

没有答案