SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery+ASP.NET won't show data on webpage

    Hello,

    i'm beginner ing ASP.NET and I tried jQuery AJAX framework with ASP.NET. This code is working fine on my computer, but doesn't display anything on the web.

    The link is http://jaxx.pri.ee

    And my code:

    Default.aspx
    Code:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!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>jaxx.pri.ee</title>
        <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
        <script type="text/javascript" src="js/jquery.timers.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $(document).everyTime(1000, function() {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "Default.aspx/GetTime",
                        data: "{}",
                        contentType: "application/json; charset=utf-8",
                        success: function(time) {
                            $("#time").html("Ja hetkel on kell " +time.d);
                        }
                    });
    
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "Default.aspx/GetDate",
                        data: "{}",
                        contentType: "application/json; charset=utf-8",
                        success: function(date) {
                            $("#date").html("Tänane kuupäev on " + date.d);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="date">
        </div>
        <div id="time"></div>
        </form>
    </body>
    </html>
    Default.aspx.cs
    Code:
    using System;
    using System.Web.Services;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        [WebMethod]
        public static string GetTime()
        {
            return DateTime.Now.ToLongTimeString();
        }
    
        [WebMethod]
        public static string GetDate()
        {
            return DateTime.Now.ToShortDateString();
        }
    }
    Maybe anyone can help me and tell me what's wrong?

  2. #2
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose you want to call a web service?

    It's much easier to use an handler (ashx) to get the date time.

  3. #3
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you post me a simple example?

  4. #4
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    mysite.com/DateTimeHandler.ashx

    Code Csharp:
    public class DateTimeHandler : IHttpHandler
    	{
     
    		#region IHttpHandler Members
     
    		public bool IsReusable
    		{
    		  return true; 
    		}
     
    		public void ProcessRequest(HttpContext context)
    		{
    context.Response.ContentType = "text/plain";			
    context.Response.Write(  DateTime.Now.ToLongTimeString());
    		}
     
    		#endregion
    	}

  5. #5
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Default.aspx
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>jaxx.pri.ee</title>
        <script language="javascript" type="text/javascript" src="js/jquery-1.3.2.js"></script>
        <script language="javascript" type="text/javascript" src="js/jquery.timers.js"></script>
        <script language="javascript" type="text/javascript">
            $(document).ready(function() {
                $(document).everyTime(1000, function() {
                    //                $.ajax({
                    //                    type: "POST",
                    //                    dataType: "json",
                    //                    url: "Default.aspx/GetTime",
                    //                    data: "{}",
                    //                    contentType: "application/json; charset=utf-8",
                    //                    success: function(time) {
                    //                        $("#time").text("Ja hetkel on kell " +time.d);
                    //                    }
                    //                });
    
                    //            $.ajax({
                    //                type: "POST",
                    //                dataType: "json",
                    //                url: "TimeDate.ashx",
                    //                data: "{}",
                    //                contentType: "application/json; charset=utf-8",
                    //                success: function(time) {
                    //                    alert(time);
                    //                    $("#time").text("Hetkel on kell " + time);
                    //                }
                    //            });
                    $.getJSON("TimeDate.ashx", function(data) {
                        alert(data);
                        $("#time").text(data.d);
                    });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="date"></div>
        <div id="time"></div>
        </form>
    </body>
    </html>
    TimeDate.ashx
    Code:
    <&#37;@ WebHandler Language="C#" Class="TimeDate" %>
    
    using System;
    using System.Web;
    
    public class TimeDate : IHttpHandler {
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/plain";
            context.Response.Write(DateTime.Now.ToLongTimeString());
        }
     
        public bool IsReusable {
            get {
                return true;
            }
        }
    }
    It still not working, I don't get it what I'm doing wrong. Any ideas guys?
    Website http://jaxx.pri.ee

  6. #6
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The date is returned as plain/text Use $.get without JSON.

  7. #7
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, now it's getting the time string. But it's not working in IE8. It works fine in Firefox. What is the problem now? And let's assume I've got a time and date to return from HttpHandler. How would I make my code then? Then I can use getJSON?

    NB! Actually the time updates himself after a 1:30 minutes. Maybe it's messed with cache somehow?

  8. #8
    SitePoint Addict evilone's Avatar
    Join Date
    Oct 2004
    Location
    Estonia
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I already got that. I clear the cache in my ServiceHandler and now it works.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •