0

I am sending the JSON request to the Node.js API.

$.ajax({
    url : url,
    contentType: "application/json",
    type: "POST",
    data: {
        "userId":1002,
        "type":"from",
        "actors": [1001],
        "actual_amount":5.00,
        "last_modified":1421480903128
    },
    success : function(data) {
        //Success
    },
    error : function(err) {
        //Error
    }
});

Request Obj received in Node.API :

actors: "[1001]"
actual_amount: "5.00"
last_modified: "1421480903128"
type: "from"
userId: "1002"



var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');


app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(bodyParser.urlencoded({ extended: true }));

Please let me know how to resolve this. I am new to Node.js

3
  • Is your question simply how to convert an array into string? - JSON.stringify. Or are you experiencing a problem doing so? Commented Jan 17, 2015 at 9:11
  • I am getting the req.body as an JSON object but the value of actors is String. Its should be an array as i am sending in the AJAX request. Commented Jan 17, 2015 at 9:14
  • Even numbers got converted to String. Commented Jan 17, 2015 at 9:15

1 Answer 1

1

You need to JSON.stringify your data object so that it's sent as JSON instead of just a string of key=value parameters:

$.ajax({
    url : url,
    contentType: "application/json",
    type: "POST",
    data: JSON.stringify({
        "userId":1002,
        "type":"from",
        "actors": [1001],
        "actual_amount":5.00,
        "last_modified":1421480903128
    }),
    success : function(data) {
        //Success
    },
    error : function(err) {
        //Error
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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.