0

I am making an AJAX call to a Struts2 Action class via jQuery ajax.

I am sending the json data to the server from my client. If I use "GET" in $.ajax(), the data is transferred to server. If I replace GET with "POST" in $.ajax(),the data is not transferred to server.

My code,

$.ajax(
{
  type: 'GET',
  url: 'login.action',
  contentType: "application/json; charset=utf-8",
  dataType: 'json',
  data:
  {
    jsonData: JSON.stringify(
    {
      number:"10",
      name:"ramesh"          
    })

  },
  success: function (data)
  {
     printStudentDetails(data);
  }
});

In firebug, while using GET,the json data is visible as

jsonData    {"number":"10","name":"ramesh"}

While using POST,the json data is visible as like this,

jsonData=%7B%22number%22%3A34%2C%22name%22%3A34%2C%22

What is the solution to send the data to the server using POST.

2
  • stackoverflow.com/questions/19544324/… Commented Oct 23, 2013 at 15:34
  • You have an extra comma in the object your passing to JSON.stringify. Firefox won't choke on it, but IE will. Commented Oct 23, 2013 at 15:53

2 Answers 2

1

It seems to be working exactly as I would expect it to. It is stringifying your JSON data, and what you're seeing in the POST is HTML encoded. If you want to send your JSON data directly, get rid of the JSON.stringify.

$.ajax(
{
  type: 'GET',
  url: 'login.action',
  contentType: "application/json; charset=utf-8",
  dataType: 'json',
  data:
  {
      number:"10",
      name:"ramesh"
  },
  success: function (data)
  {
     printStudentDetails(data);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

You aren't sending JSON to the server, you're sending form params. remove the contentType.

url: 'login.action',
//contentType: "application/json; charset=utf-8",
dataType: 'json',

5 Comments

And you're sure this has nothing to do with how you are retrieving the data on the server? i guarantee it is sending the data to the server in one way or another.
in server,iam using java struts2 action class,
and i'm not familiar with that language
i am sure it is not sending to the server bcoz at server side i tested it
you proved that it is sending the data in your question. You can see the data in firebug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.