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

  • SEARCH
  • Home
  • 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 8343155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:58:23+00:00 2026-06-09T05:58:23+00:00

I have a jquery script in my file that says this: <script type=text/javascript> $(document).ready(function(e){

  • 0

I have a jquery script in my file that says this:

<script type="text/javascript">
$(document).ready(function(e){
    $('#myButton').click(function(e){
        var formdata = {name: 'Alan', hobby: 'boxing'};
        var submiturl = 'http://localhost/cake/gronsters/testJSON/';
        $.ajax({
            type: "POST", 
            url: submiturl,
            data: formdata,
            success: function(message){
                console.log(message);
            }
        });
        e.preventDefault();
    })
});

Then I have a php script at testJSON that says this:

public function testJSON(){
    $name = $this->request->data['name'] . '-Sue';
    $hobby = $this->request->data['hobby'] . ' for donuts';
    $data = array(  'name' => $name, 'hobby' => $hobby);
    echo json_encode($data);
}

The place where console.log looks for message gives me {“name”:”Alan-Sue”,”hobby”:”boxing for donuts”}, which seems correct, except that it’s immediately followed by the full html of my web page. And if I try console.log(message.name) it says ‘undefined.’

What’s going on 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-09T05:58:24+00:00Added an answer on June 9, 2026 at 5:58 am

    You’re on the right path, although as others have mentioned, it seems like you are using a MVC framework for your website and it is including the content of your webpage after displaying the JSON string. jQuery/JavaScript cannot parse JSON if there are other random text or characters in the response. You will need to use exit; or die(); after the JSON is echo’ed as the following example shows…

    public function testJSON(){
        $name = $this->request->data['name'] . '-Sue';
        $hobby = $this->request->data['hobby'] . ' for donuts';
        $data = array(  'name' => $name, 'hobby' => $hobby);
        // Will halt the script and echo the json-encoded data.
        die(json_encode($data));
    }
    

    Also, you will want to make sure that jQuery is parsing the response as JSON. By default it will attempt to make an intelligent guess of which type of data is being returned, but you cannot always rely on that. You should add the dataType option to the AJAX call like in the following example…

    <script type="text/javascript">
    $(document).ready(function(e){
        $('#myButton').click(function(e){
            // It is best practice to "preventDefault" before any code is executed.
            // It will not cause the ajax call to halt.
            e.preventDefault();
            var formdata = {name: 'Alan', hobby: 'boxing'};
            var submiturl = 'http://localhost/cake/gronsters/testJSON/';
            $.ajax({
                type: "POST", 
                url: submiturl,
                data: formdata,
                // This is the proper data type for JSON strings.
                dataType: 'json',
                success: function(message){
                    // JSON is parsed into an object, so you cannot just output
                    // "message" as it will just show "[object Object]".
                    console.log(message.name);
                }
            });
        });
    });
    </script>
    

    I’ve included some comments in the code to help better explain. Hopefully this will help you out a bit more. You also do not need to using the full $.ajax function unless you plan on using error handlers or any other of the more advanced options within that function. Alternatively, you can shorten your code by using $.post and accomplish the same as your original example with less code.

    <script type="text/javascript">
    $(document).ready(function() {
        $('#myButton').click(function(e){
            e.preventDefault();
            var formdata = {name: 'Alan', hobby: 'boxing'};
            var submiturl = 'http://localhost/cake/gronsters/testJSON/';
    
            $.post(submiturl, formdata, function(message) {
                console.log(message.name);
            });
    
        });
    });
    </script>
    

    More options and usage information about $.ajax jQuery.ajax();
    More information about $.post jQuery.post();

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

Sidebar

Related Questions

I have a simple jquery click event <script type="text/javascript"> $(function() { $('#post').click(function() { alert("test");
I have this jQuery script that works fine: $(select).change(function () { var finalPrice =
I have the following jquery script: $(function(){ $('#top-menu').on('click', 'a.change-menu', function(e){ e.preventDefault() $(#menu-change-div).load($(this).attr(href)); }); });
Update: I did get this working, here is the new javascript: $(document).ready(function() { $('a').click(
I have a php file that runs a simple jquery script: var text =
I have a jQuery script: $.ajax({ url: /scripts/secure/development/ajax.asp, type: POST, dataType: text, data: cmd=addresses,
I have this jQuery script var dataString = class_id=+class_id; $.ajax({ type: POST, url: page.php,
My header is calling a javascript file which sends out an email: <script type=text/javascript
I have this jQuery script that sends a username an password to a PHP
I have this jquery script to call an external file. So far so good.

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.