0

I am passing a JSON object like

[{"name":"SHIFT"},{"name":"CONVERSION"},{"name":"VARIATION"}] 

and POST request is

 $.ajax({
    url: 'myUrl',
    type:'POST',
    dataType: "json",
    cache: false,
    timeout: 5000,
    data:json,//passing a json obj
     success: function(data, textStatus, jqXHR) { alert("success") },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(jqXHR);
    }
 });   

I am using express within my Node module and below is my node.js code

var express = require('express');
var  http = require('http');
var path = require('path');
var app=express();
var app=express.createServer();
app.use(express.bodyParser());
app.use(app.router);

  app.post("/",function(request,response) {              
     console.log(request);  
     response.header('Access-Control-Allow-Origin', "*");
     console.log(request.body)
    // response.send(request.body); 
 }); 

How do I get the JSON string and parse it in app.post method ? I tried request.body but it provides me with the following

{"SHIFT":"","CONVERSION":"","VARIATION":""}
Praveen
  • 50,562
  • 29
  • 125
  • 152
misthacoder
  • 127
  • 1
  • 1
  • 11
  • See this http://stackoverflow.com/questions/11994829/node-js-parse-json-of-request and http://stackoverflow.com/questions/10005939/how-to-consume-json-post-data-in-an-express-application – Amila Iddamalgoda Mar 13 '14 at 05:14
  • I have already referred it, but still i am getting the same response. – misthacoder Mar 13 '14 at 05:15
  • Please post how you pass the json object. $.ajax({ data:json,//passing a json obj }); – Amila Iddamalgoda Mar 13 '14 at 05:23
  • json is a variable that I have used. var json=[{"name":"SHIFT"},{"name":"CONVERSION"},{"name":"VARIATION"}] – misthacoder Mar 13 '14 at 05:26
  • That isn't JSON, that is an array of objects. Wrap it in a `JSON.stringify();` – Kevin Reilly Mar 13 '14 at 08:10
  • @Kevin. i wrapped up in the array of objects ie [{"name":"SHIFT"},{"name":"CONVERSION"},{"name":"VARIATION"} in JSON.stringify().How do I parse it in node.js level once i do the post request.? – misthacoder Mar 13 '14 at 08:38
  • `var data = JSON.parse(request.body);` – Kevin Reilly Mar 13 '14 at 08:40
  • @KevinReilly when i do console.log in node.js for request.body, it is null. – misthacoder Mar 13 '14 at 08:46
  • You should be able to inspect the request on the client side to see what the client is sending (IE: Network tab in developer tools). I think on the jquery side you might need to set a header saying the content type is application/json. You might also want to `console.log(request);` for fun. – Kevin Reilly Mar 13 '14 at 08:49
  • I now see what the problem is. The `data` key of `$.ajax` is, by default, used for a simple query string like `foo=bar&beep=boop`. If you make your array of objects into an object of key-vale pairs, it should work properly without JSON. – Kevin Reilly Mar 13 '14 at 08:53
  • @KevinReilly, yes I have thought about this. but the issue for me is i have to populate the array in an each fucntion, which populates json.push({name:$(this).text()});.AFter which i have done JSON.stringify(json). – misthacoder Mar 13 '14 at 09:03

1 Answers1

0
 $.ajax({
    url: 'myUrl',
    type:'POST',
    dataType: "json",
    cache: false,
    timeout: 5000,
    data:{
    jsonData: "{ \"name\": \"SHIFT\", \"name\": \"CONVERSION\",\"name\": \"VARIATION\" }"
    // or jsonData: JSON.stringify(credentials)   (newest browsers only)
    },
     success: function(data, textStatus, jqXHR) { alert("success") },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(jqXHR);
    }
 });  
Amila Iddamalgoda
  • 3,664
  • 10
  • 42
  • 78
  • how do i get this format of json string. I have a div and I want all the contents of div to be inserted in a json object $("div ol li span[class=title]").each(function (e) { //var arr={}; //arr["name"]=$(this).text(); //jsonStr.push(arr); jsonStr.push({name:$(this).text()}); }); var json = JSON.stringify(jsonStr); – misthacoder Mar 13 '14 at 05:31
  • var temp = new Object(); temp["name"] =$(this).text(); jsonStr.push(temp); – Amila Iddamalgoda Mar 13 '14 at 05:42