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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:07:04+00:00 2026-05-24T14:07:04+00:00

Very beginner level question. Trying to learn JSON and having trouble with getting proper

  • 0

Very beginner level question. Trying to learn JSON and having trouble with getting proper return data. I’m trying to use JSON to return errors from a php form using json_encode($errs, JSON_FORCE_OBJECT), but first I want to make the very simple code below work to understand JSON Objects.

I’ve viewed similar threads here, read the entire JSON.org documentation, looked at JQuery’s $.getJSON() method and plugged in the exact code from the documentation, as well as the exact code for the JSON documentation, and I still can’t seem to get a response other than “undefined” or “[object, Object]”.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!---CSS-->
    <link href="style/960.css" media="all" rel="stylesheet" type="text/css" />
    <link href="style/pubstyle.css" media="all" rel="stylesheet" type="text/css" />
    <!--- Javascript -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" 
    type="text/javascript"></script>
    <script src="javascripts/form.js" type="text/javascript"></script>
    <script src="javascripts/validate.min.js" type="text/javascript"></script>
    <script src="javascripts/json2.js" type="text/javascript"></script>
    </head>
    <body>
    <script type="text/javascript">
         $(document).ready(function() {
             var resp = {"message":"hi"};
         $("a#button").click(function(resp) {
           alert(resp)
         });
         });

    </script>

    <a id="button" href="#">Button</a>
    </body>
    </html>

The code above returns an alert with “[object Object]”. When I change the code to:

    $(document).ready(function() {
             var resp = {"message":"hi"};
         $("a#button").click(function(resp) {
           alert(resp.message)
         });
         });

I get an “undefined” alert. I tried putting it into an array object

{ "messages": [ {"message":"hi"}]}

and calling it with

resp.messages[0].message

with no luck. I tried change the Content-Type to “application/json” and “text/plain” and neither worked. Also tried

$(document).ready(function() {  
var resp = jQuery.parseJSON('{"message":"hi"}');
$("a#button").click(function(resp) {
alert(resp.message)
});
});
  • 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-24T14:07:05+00:00Added an answer on May 24, 2026 at 2:07 pm

    You’re confusing a couple different elements here.

    Simple example, based on the last code chunk in your question:

    $(document).ready(function() {  
        var resp = $.parseJSON('{"message":"hi"}');
        $('#button').click(function(event) {
            alert(resp.message);
        });
    });
    

    Your version of this code didn’t work because the click callback function receives the click event object as its argument. The resp argument name in the callback shadowed the resp variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very beginner in Flash. I'm trying to use document class only. I created
I am a beginner level php programmer. I was trying to use the MVC
I am a very beginner to Java, recently trying to use some existing libraries
Once again a very beginner-ish question, but here I go: I would like to
The very common beginner mistake is when you try to use a class property
Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET
I am a beginner of python and have a question, very confusing for me.
I came here to ask a simple, very beginner question regarding python. I just
i know this question was asked before but i have this very weird beginner
This is a very beginner question. I have tried to search for advice but

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.