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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:44:48+00:00 2026-06-16T05:44:48+00:00

I am new to web services.I have the following code and when I try

  • 0

I am new to web services.I have the following code and when I try to run it on the local host I am not geting the required output…it shows FirstName:undefined LastName:undefined..

following is my code

$(document).ready(function(){
    var employees = [
  { "firstName":"John" , "lastName":"Doe" }
];
$.ajax({
    url: "MyService.php",
    type: "POST",
    data: employees,
    dataType: "json",
    success:function(data) {
        alert("Firstname:"+data.firstName+", LastName: "+data.lastName);
        },
    error:function(data) {
        alert('error');
    }  });
});

The php code is

echo json_encode($_POST);
  • 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-16T05:44:49+00:00Added an answer on June 16, 2026 at 5:44 am

    The value of data should be an object. You are passing an array containing an object. Get rid of the array.

    This:

        var employees = [
      { "firstName":"John" , "lastName":"Doe" }
    ];
    

    … is submitting the following to the server:

    undefined=
    

    It should be:

    var employees = { 
        "firstName": "John",
        "lastName":"Doe"
    };
    

    which would submit this:

    firstName=John&lastName=Doe
    

    Additionally, you don’t appear to be specifying a Content-Type header, so PHP is defaulting to text/html. This is forcing you to write JavaScript that says “Don’t believe the server, threat this as JSON and not HTML”.

    You can fix that with:

    header('Content-Type: application/json');
    echo json_encode($_POST);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to iPhone application development, i have used the web services and
I am very new Android Soap Web-services I have some knowledge on consuming soap
I have the following code in a java Web Service: public boolean makeFile(String fileName,
When I try the following lookup in my code: Context initCtx = new InitialContext();
I am totally new to web services and cannot get mine to work. My
I am new to web services and below I am sharing my understanding so
I'm pretty new to Web Services in java. I was starting on a project
I am new to web services. Please give suggestions how to insert and retrieve
I am a new iPhone developer and totally new to web-services as well. I
I am new to using web services. I am assigned a task in which

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.