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 8688429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:21:39+00:00 2026-06-12T23:21:39+00:00

So I’m working on getting some data with AJAX. All appears to work okay,

  • 0

So I’m working on getting some data with AJAX. All appears to work okay, but I’m not actually getting the data on my page:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
    function getMailDetail(mailId) {
        $.ajax({
            type: "GET",
            url: "GetMail.ashx",
            data: "mid=" + mailId,
            success: function (data) {
                console.log(data);
                var pnlMail = $('#<%= pnlMail.ClientID %>');
                var lblFrom = $('#<%= lblFrom.ClientID %>');
                var lblDate = $('#<%= lblDate.ClientID %>');
                var lblSubject = $('#<%= lblSubject.ClientID %>');
                var lblMessage = $('#<%= lblMessage.ClientID %>');

                lblFrom.text(data.From);
                lblDate.text(data.Date);
                lblSubject.text(data.Subject);
                lblMessage.text(data.Message);

                pnlMail.css("display", "block");
            }
        });
    }
</script>

I’ve logged data to check it’s value. As this is my first try at this, I’m not exactly sure what to expect, however, I believe I should be getting back name:value pairs. Currently the console is logging absolutely nothing. No value at all.

Here’s my HttpHandler:

    public void ProcessRequest(HttpContext context)
    {
        string mailid = context.Request.QueryString["mid"].ToString();
        context.Response.ContentType = "text/json";

        context.Response.Write(showMailDetail(mailid));
    }

    protected string showMailDetail(string id)
    {
        int mailid = int.Parse(id);

        string From = "";
        DateTime Date = DateTime.Now;
        string Subject = "";
        string Message = "";

        MySqlContext db = new MySqlContext();

        string sql = "select m.datesent, m.subject, m.message, u.firstname, u.lastname from mail m inner join users u on m.sender = u.userid where m.mailid = @id";

        List<MySqlParameter> args = new List<MySqlParameter>();
        args.Add(new MySqlParameter() { ParameterName = "@id", MySqlDbType = MySqlDbType.Int32, Value = mailid });

        MySqlDataReader dr = db.getReader(sql, args);

        if (dr.HasRows)
        {
            dr.Read();

            From = (string)dr["firstname"] + " " + (string)dr["lastname"];
            Date = dr.GetDateTime("datesent");
            Subject = (string)dr["subject"];
            Message = (string)dr["message"];
        }
        dr.Close();

        string result = "{ 'From' : " + From + ", 'Date' : " + Date.ToString("yyyy/MM/dd HH:mm:ss") + ", 'Subject' : " + Subject + ", 'Message' : " + Message + " }";

        return result; 
    }

Can anyone help me figure out why I’m not getting anything here? I’m completely prepared for the possibility that I’m doing this wrong…

I wrote the code based on the article at
http://www.codeproject.com/Articles/170882/jQuery-AJAX-and-HttpHandlers-in-ASP-NET

EDIT
Found that I was using an incorrect column name in my sql – should have been m.sender not m.senderid … However, I now have another problem.

When I trigger the Handler, I get the following in Chrome’s console:

GET GetMail.ashx?mid=1 500 (Internal Server Error) jquery.min.js:2

EDIT 2
Corrected more mistakes in the code on the Handler and now there are no errors appearing anywhere, but it doesn’t look like the click is actually triggering anything at all…

Not sure how to proceed here…

  • 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-06-12T23:21:40+00:00Added an answer on June 12, 2026 at 11:21 pm

    Wow, never manually build JSON by using string concatenations, you’re never gonna make it right.

    Always use a serializer when you are working with JSON:

    public void ProcessRequest(HttpContext context)
    {
        int mailid = int.Parse(context.Request["mid"]);
        var detail = GetMailDetail(mailid);
    
        if (detail != null)
        {
            context.Response.ContentType = "application/json";
            string json = new JavaScriptSerializer().Serialize(detail);
            context.Response.Write(json);
        }
        else
        {
            context.Response.StatusCode = 404;
        }
    }
    
    protected object GetMailDetail(int mailid)
    {
        string sql = "select m.datesent, m.subject, m.message, u.firstname, u.lastname from mail m inner join users u on m.senderid = u.userid where m.mailid = @id";
    
        var args = new[]
        {
            new MySqlParameter 
            { 
                ParameterName = "@id", 
                MySqlDbType = MySqlDbType.Int32, 
                Value = mailid 
            }
        }.ToList();
    
        MySqlContext db = new MySqlContext();
        using (MySqlDataReader dr = db.getReader(sql, args))
        {
            if (dr.Read())
            {
                return new 
                {
                    From = string.Format("{0} {1}", dr["firstname"], dr["lastname"]),
                    Date = dr.GetDateTime("datesent").ToString("yyyy/MM/dd HH:mm:ss"),
                    Subject = dr["subject"],
                    Message = dr["message"]
                }
            }
            return null;
        }
    }
    

    Now let’s see the client side call. You seem to have defined some getMailDetail function but you don’t seem to be calling it anywhere, or at least you haven’t shown it.

    Here’s a full example to try:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">
        function getMailDetail(mailId) {
            $.ajax({
                type: 'GET',
                url: '<%= ResolveUrl("~/GetMail.ashx") %>',
                data: { mid: mailId },
                success: function (data) {
                    console.log(data);
                }
            });
        }
    </script>
    
    <div onclick="getMailDetail(5);">Click to get mail detail</div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

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.