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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:26:33+00:00 2026-06-05T05:26:33+00:00

I am having trouble sending a JavaScript objects over a http request. The http

  • 0

I am having trouble sending a JavaScript objects over a http request. The http endpoing only accepts json content-type (“application/json” or “text/json”)

I am not sure why
data2 ( stringified json ) works fine
But, data1 ( json object ) throws http 400 Bad Request.
i.e why is jQuery not serializing the json object to a valid json string for the server to process.

var data1 = ({ rating : 3 });  //does not work
var data2 = '{ "rating" : 3}'; //works fine

$.ajax({
    url : "/rate",
    data : data1,
    type : "POST",
    contentType: "application/json",
    success: function(json){
        console.log("Ajax Return :"+json);
    }
});
  • 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-05T05:26:35+00:00Added an answer on June 5, 2026 at 5:26 am

    By default data parameter values that is part of any ajax jQuery call, converts the JS object into a url-form-encoding i.e “param1=value&param2=value” string. This is the case for both GET and POST requests.

    Interestingly, even if you explicitly specify { contentType : “application/json” } in the ajax method call, only the appropriate header is set, the JS object you pass as value for the data parameter is not converted to a JSON string ( one always hopes for more intelligence ), but still gets encoded as url-form-encoding. So, one has to explicitly convert the JS object to JSON string.

    There are a options for doing so,

    1. JSON.stringify(obj); Its part of Javascript, I believe comes comes from ECMA 5 standard. Easiest, but the down side is it does not work in older browsers IE6 and before.
    2. jQuery json plugin that also has some additional features.
    3. Code from json.org

    So, now my POST request that needs a json body would work like this.

    var dataStr = JSON.stringify({ rating : 3 });  
    
    $.ajax({
        url : "/rate",
        data : dataStr,
        type : "POST",
        contentType: "application/json",
        success: function(json){
            console.log("Ajax Return :"+json);
        }
    });
    

    Note:
    Effect of “processData” boolean parameter
    Some answer here mentioned that one has to set { processData : false }. But, this is not the case. This has an effect only when the type of “data” parameter is not a string. If its not a string then the default behavior is to convert the Object into url-form-encoding. But, this is mostly useless I think because if you pass a regular Object and processData is set to false, then it tries to do (data).toString which results in “[Object] [Object]” which is pretty useless.

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

Sidebar

Related Questions

I'm having trouble sending out a simple HTTP request using Actionscript 3's Socket() object.
I am having trouble sending JSON to a WebMethod. Here is the way that
I'm having trouble figuring out how to display some return JSON objects. My script
I am having trouble sending data on a socket from an iphone application I
I'm having trouble sending an UIImage object to my REST web service that only
I was having trouble sending up a url containing accent characters using IE. Here's
as indicated by the title I am having trouble sending an email via my
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
I'm having trouble sending data to a different view using the prepareForSegue method… I
Having some trouble sending properly formatted HTML e-mail from a PHP script. I am

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.