首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

$ajax没法调用数据

2013-04-22 
$ajax无法调用数据 var chartfunction requestData() {$.ajax({type: Post,url: Test1.aspx/DataBank

$ajax无法调用数据

 var chart;
        function requestData() {
            $.ajax({
                type: "Post",
                url: "Test1.aspx/DataBank",

                contentType: "application/json; charset=utf-8",

                dataType: "json",
                success: function (data) {
                    var x = (new Date).getTime(),
                    y = data.Data[0].linespeed;
                    chart.series[0].addPoint([x, y], true, true);
                    // call it again after one second
                    setTimeout(requestData, 1000);
                },
                error: function (err) {
                    alert(err + "调用后台程序出现错误,请尝试刷新!");

下面是Test.aspx.cs文件中的DataBank函数
 
public string DataBank()
        {
            string json = "{"Data":[";
            if (!flag)
            {
                json += "{"linespeed":"" + "0" + ""}]}";
            }
            else
            {
                json += "{"linespeed":"" + iDataChannels.getLineSpeed().ToString() + ""}]}";
            }
            return json;
        }

问题就是启动网页时,$ajax无法成功加载数据。分不多,各位大大,请帮忙看看呀,这里拜谢了。
[解决办法]
前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function CallCs() {
            $.ajax({
                //要用post方式      
                type: "Post",
                //方法所在页面和方法名      
                url: "WebForm1.aspx/GetJson",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    //返回的数据用data.d获取内容      
                    alert(data.d);
                },
                error: function (err) {
                    alert(err);
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" onclick="CallCs()" value="test" />
    </div>
    </form>
</body>
</html>



后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string GetJson()
        {
            return "Hello world";
        }
    }


}

热点排行