Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1078355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:42:40+00:00 2026-05-16T21:42:40+00:00

The javascript does not get value from the web method. It says undefined for

  • 0

The javascript does not get value from the web method.

It says undefined for the value of s located in CallMe()..

My aim is get an object from the web method…. to use the data in js..

What am I missing_?

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Services;

public partial class _Default : System.Web.UI.Page 
{
    Label lblGeneral;
    protected void Page_Load(object sender, EventArgs e)
    {
        lblGeneral = textMessager;
    }
    [System.Web.Services.WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string ShowMessage()
    {

        return ExternalManager.Write();
    }
}

js

// JScript File
function CallMe()
 {    
     // call server side method
     var s = PageMethods.ShowMessage();
     s = eval(s);
 }



(function() {
var status = true;
var fetchService = function() {

           if(status){
             CallMe();
            status = false;
            }


            setTimeout(fetchService, 5000);
           status = true;

        }

window.onload = fetchService;
}())

****The Util Class ******

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
/// <summary>
/// Summary description for ExternalManager
/// </summary>
public class ExternalManager
{
    public ExternalManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static string Write()
    {
        string s = "Okay Buddy" + DateTime.Today.ToLongDateString();
        JavaScriptSerializer jss = new JavaScriptSerializer();
        string serializedPerson = jss.Serialize(s);
        return  s;
    }
}

Used Tech:
Asp.net 2.0 + Ajax Enabled
C#

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:42:41+00:00Added an answer on May 16, 2026 at 9:42 pm

    It’s an AJAX call. You cannot write:

    var s = PageMethods.ShowMessage();
    

    and expect to use the s variable immediately because the call is asynchronous (it returns immediately but the result is available only later after the server responds). You need to use the callback. Here’s a full working example:

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Web.Services" %>
    <%@ Import Namespace="System.Web.Script.Services" %>
    <script type="text/C#" runat="server">
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string ShowMessage()
        {
            return "Okay Buddy" + DateTime.Today.ToLongDateString();
        }
    </script>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
            function CallMe() {
                // call server side method
                PageMethods.ShowMessage(function (result) {
                    alert(result);
                });
            }
    
            (function() {
                var status = true;
                var fetchService = function () {
                    if (status) {
                        CallMe();
                        status = false;
                    }
                    setTimeout(fetchService, 5000);
                    status = true;
                }
    
                window.onload = fetchService;
            }());
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true" />
        </form>
    </body>
    </html>
    

    Also notice that you should not use JavaScriptSerializer manually. It is used internally by PageMethods. In your ShowMessage method you could also return complex objects.

    If the method had for example two arguments you would pass them before the success and error callbacks:

    PageMethods.ShowMessage('param1', 'param2', onSucceed, onError); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firefox 3.5 does not allow creating java OBJECT tag with Javascript (document.write)? this technique
Since JavaScript is not derived from Java, why does it have Java in the
Facebook does not get information from description Open Graph tags I do get following
How can you get the selected value from drop down list using JavaScript? I
It looks like JavaScript does not have access to authentication cookies ('ASP.NET_SessionId', '.ASPXFORMSAUTH') in
This javascript code does not work in IE8, but works in Firefox and Google
I'm using JSF templates and Primefaces. Javascript code does not seem to be working
The API documentation does not contain an obvious way to trigger a javascript event
I have the following javascript in my page which does not seem to be
I have a partial view which contain JavaScript and Ajax, but does not run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.