Click here to Skip to main content
15,888,340 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi everyone,

I have JQuery code on my Default.aspx page :

JavaScript
<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
  <script type="text/javascript">

       $(document).ajaxError(function(){
           if (window.console && window.console.error) {
               console.error(arguments);
           }
       });

       $(document).ready(function () {

           var msgbox = $("#status");

           $("#Button1").click(function () {

               $.ajax({

                   type: "POST",
                   //Page Name (in which the method should be called) and method name
                   url: "Default.aspx/GetDateVal",
                   // If you want to pass parameter or data to server side function you can try line
                   //data: "{'args':'Pankaj'}",
                   //else If you don't want to pass any value to server side function leave
                   data: "{}",
                   contentType: "application/json; charset=utf-8",
                   dataType: "json",
                   success: function (msg) {
                       //Got the response from server and render to the client
                       msgbox.html(msg.d);

                   }

               });

           });

       });

</script>


I have the below code in code-behind class:

C#
[System.Web.Services.WebMethod]
        public static string GetDateVal()
        {
            return DateTime.Now.ToString("dd/MMM/yyyy HH:mm:ss");

            //return "<input id='test1' value='wsf' />";
        }


This works quite fine when running in localhost from VS2010 ie. returns date string as expected but when running in localhost from VS2003 I get no result back from the method?

Anyone know why this would be so?

Thanks.
Posted

Same version of .Net framework is my first question? Also, why on earth are you trying to make a request to a url with forward slashes after the file name???? Use query parameters, that's what they are there for! You're url may well be seen by VS2003 as invalid and so return an error page. Your URL should be Default.aspx?Method=GetDateVal and in your code behind do something like

C#
if(Request["Method"] == "GetDateVal") 
{ 
    Response.Clear(); 
    Response.ContentType = "text"; 
    Response.Write(GetDateVal()); 
    Response.End(); 
}


It looks like this is just an example of jQuery ajax methods - you should certainly never get the time from the server in a practical application. Two reasons: 1 time zones and time differences between both your user and your own server(s) (and between one of your serevrs and another server) - always store stuff in databases for example by UTC time and then convert to local time on display. Secondly: It would put a huge unnecessary load on your server!

Anyway, hope this helps, Ed :)

Edit: Also, seen this a lot recently, charset should NOT be defined in the content type - where ever this is coming from has got it wrong and charset has it's own header - so far as I'm aware jQuery will not split up the ContentType passed to it into these two separate headers for you.
 
Share this answer
 
v2
Hi Edward thank you for your response.

Yes, they are running on different versions of .Net Framework.
Will attempt to pass through the Method name as a parameter rather after a '/' and see if this would work for 2003 and let you know. (Attempted this still same result though)

The fact that the function of method returned the time was just for test purposes and not an actual for use method :-). Appreciate you pointing out the detriments of this though for others who may come across this :-)

Agreed, i have removed the charset from the contenttype property.

I have managed to get this working and will issue a post soon detailing how I did it.

Thanks.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900