Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post jQuery JSON Object to NodeJs Restify

I want to know why it is so hard to post a simple JSON string in a /:parameter to restify. I have followed many examples but have not found anything concrete.

I have the following code in the front end.

$("#btnDoTest").click(function() {

    var jData = {
        hello: "world"
    };
    var request = $.ajax({
        url: "http://localhost:8081/j/",
        async: false,
        type: "POST",
        data: JSON.stringify(jData),
        contentType: "application/javascript",
        dataType: "json"
    });


    request.success(function(result) {

        console.log(result);

    });

    request.fail(function(jqXHR, textStatus) {
        alert("Request failed: " + textStatus);
    });


});

I am succesful in sending simple text if I concatenate the param after the j/. But what I want to send is an object like this {hello:"world"} and reconstruct it back in nodeJS and work with it.

--Edit:

This is my nodejs file
/* the below function is from restifylib/response.js */
var restify = require("restify");

/* create the restify server */
var server = restify.createServer({

});


server.use(restify.bodyParser({ mapParams: true }));

server.use(
  function crossOrigin(req,res,next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    return next();
  }
);


 server.post('/j/', function (req, res, next) {


   //res.send(201,"REceived body: "+JSON.stringify(req.params));
   res.send(201,"REceived body: "+JSON.stringify(req.params));
   return next();
 });


var port = 8081;
server.listen(port);
console.log("Server listening on port " +port)

Any help would be appreciated thanks.

0x

like image 718
Oxnigarth Avatar asked Jun 04 '13 15:06

Oxnigarth


People also ask

What is restify JS and MySQL?

Restify js : restify is a rest framework for building web applications on top of Node.js MySQL : This nodejs module help to create connection with MySQL database and allow SQL queries with table

Is node server using restify for API?

The node server is using RESTify for its API. I am having trouble getting req.body.name from the body of the posted data. The posted data contains a json body.

How to create MySQL database connection in NodeJS REST API?

The Node js Rest API details are as follows: MySQL is very popular opensource relational database.We will create MySQL database connection into config.js file The config.js file location would be d: odejs-restify-restapi-example\config.js. We will add following code into this file.

How to install Node JS REST API in Windows 10?

lets install node js module using npm command, now open cmd window and go to path of your node application, like d:/nodejs-restify-restapi-example and run below example. Above command will install all dependency node js modules into node_modules folder. The Node js Rest API details are as follows:


1 Answers

I finally got it working.

--Front end code

$("#btnDoTest").click(function() {



        var request = $.ajax({

            url: "http://localhost:3000/j",
            async: false,
            type: "POST",
            data: {
                blob: {wob:"1",job:"2", ar:[1,2,{a:'b'}]}
            },

            contentType: "application/x-www-form-urlencoded", //This is what made the difference.
            dataType: "json",

        });


        request.success(function(result) {

            console.log(result);

        });

        request.fail(function(jqXHR, textStatus) {
            alert("Request failed: " + textStatus);
        });


    });

NodeJs services

/* the below function is from restifylib/response.js */
var restify = require("restify");

/* create the restify server */
var server = restify.createServer({

});


server.use(restify.bodyParser());
server.use(restify.CORS());


server.post('/j/', function(req, res, next) {

    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");

    // req.params  == data on jquery ajax request.


    res.send(200, JSON.stringify(req.params));
    console.log(req.params.blob.ar[2].a)



    res.end();
    return next();
});


var port = 3000;
server.listen(port);
console.log("Server listening on port " + port)
like image 80
Oxnigarth Avatar answered Sep 28 '22 16:09

Oxnigarth