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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:25:26+00:00 2026-06-10T17:25:26+00:00

When I post data to the server as JSON via AJAX, values from form

  • 0

When I post data to the server as JSON via AJAX, values from form elements come is as strings (unicode). Here’s a simple example:

<select id="test">
  <option value=1>One</option>
  <option value=2>Two</option>
  <option value=3>Three</option>
</select>

Sending data to the server like this:

$('test').change(function(){
  var id = $(this).val();
  $.ajax(
  {
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: someUrl,
    data: JSON.stringify(id:id),
    dataType: 'json',
    success: doSomething
  });

Then on the server (Pyramid 1.3.3):

log.debug(type(request.json_body['id'])) gives <type 'unicode'>

This makes code like if request.json_body['id'] == 2 False since the value is actually u’2′.

I’ve been doing id = int(request.json_body['id']) for all the params that should be ints, but is there a better way? I feel like I’m missing something obvious.

  • 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-10T17:25:27+00:00Added an answer on June 10, 2026 at 5:25 pm

    No, this is correct. Form element values in a web page are always strings, regardless of what you expect the end-users to enter.

    You can use collander to automate the conversion on the server side though. You specify a schema, pass in the decoded JSON data structure and get a nice Python structure back, fully validated.

    The simplest possible example for deserializing integers:

    >>> import colander
    >>> id = colander.SchemaNode(colander.Integer())
    >>> id.deserialize('1')
    1
    

    but colander can handle mappings, sequences, booleans, datetime objects, and much more besides.

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

Sidebar

Related Questions

I wander how send post form data in json format via ajax with JQuery
I have a AJAX post method that gets xml data from server. But when
I am using jquery post to submit data on server and data having json
I've got a form and I need to post data to the server every
I am trying to post data from PHP to Server which is built in
I am trying to setup a simple proxy server that we post the data
If I GZip the a POST request form data, will a HTTP server decompress
I want to POST an Object via JSON to node.js server. The Object structure
Via jquery, I ajax/POST this json {indices:[1,2,6]}: to a symfony2 action. Right now I
I am trying to post a json string via a form submission ( multi-part

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.