猿问

asp.net 用ajax到后台取值赋给html控件,可是取到的值一直没变?

页面js代码:
<script src="js/jquery-1.7.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        var timer;
        function progressShow()
        {
            //$("#progressSpan").text("<%=progressInt %>%");
            $.ajax({
                //url: "自写progressBar.aspx/returnProgress",
                //type: "text/plain",
                //contentType: "application/text;charset=utf-8",
                url: "ashx/returnProgress.ashx",
                success: function(data){
                    alert(data);
                    $("#progressDiv").width(data);
                    $("#progressSpan").text(data);
                },
                error: function(error){
                    alert("error: "+error);
                }
            });
        }
        window.onload = function(){ timer = setInterval(progressShow,1500);}
    </script>

 

body:
<div id="contentDiv" style="width:100px; background-color:Gray;">
        <div id="progressDiv" style="width:1px; background-color:Red;">
            <span id="progressSpan" style="color:Black;"></span>
        </div>
    </div>

 

aspx.cs:

namespace progressBar进度条
{
    public partial class 自写progressBar : System.Web.UI.Page
    {
        public static int progressInt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Thread thread = new Thread(new ThreadStart(progressUp));
                thread.Start();
            }
        }

        public void progressUp()
        {
            for (int i = 1; i < 101; i++)
            {
                Thread.Sleep(2000);
                progressInt = i;
            }
        }


        [WebMethod]
        public static string returnProgress()
        {
            //HttpContext.Current.Response.Write(progressInt);
            //HttpContext.Current.Response.End();
            return progressInt.ToString();
        }

    }
}

ashx:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class returnProgress : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(自写progressBar.progressInt);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

实际上progressInt一直在增加,可是为什么取出来的progressInt全是0?


陪伴而非守候
浏览 645回答 4
4回答

暮色呼如

url: "ashx/returnProgress.ashx"+new Date(), 试试

缥缈止盈

你确认Page_Load方法被调用了吗?

偶然的你

确定,我用timer定时取过 自写progressBar.progressInt的值,确实在自增啊

慕森王

单步调试下看看
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答