Psl
Psl

Reputation: 3920

Send json response to Jquery

Am using following code to get some response from client machine and getting that response properly .But i have to send that response to Jquery.I dont know how it is possible?"

var getOsLists = function(context){
 var options = {
            host: '172.16.2.51',
            port: 9090,
            path: '/vm/osList',
            method: 'POST'
        };
 var req = http.request(options, function (res) {
            res.on('data', function (data) {               
                    console.log(data);  

                                                   // return sendJson(data, 404);

            });
        });
        req.on('error', function (e) {
            console.error(e);
        });
        req.end();
}
exports.getOsLists = getOsLists; 

i want to get the above data in the below section

function getOsList() {
    $.getJSON('/getOpSystem', function (data) {
            alert(data.toString()); // it does nt print anything
        var html = '';
        data.forEach(function (n) {
            alert(n);
            html += '<option value="' + n.ID + '">' + n.ID + '</option>';
        });
        $('#os').html(html);
    });
}

and in node.js

case '/getOpSystem':
          objSchedule.getOsLists();
          break;

Upvotes: 1

Views: 126

Answers (4)

Michael Tang
Michael Tang

Reputation: 4926

I'd recommend using the express framework. It provides a res.json() method that lets you pass through a Javascript object. The framework will handle stringify-ing the object to JSON, which jQuery can read.

var express = require('express');
var app = express();

app.get('/getOpSystem', function(req, res) {
    var system = [];
    //make a request, put response in `system`
    res.json(system);
});

app.listen(3000);

Alternatively, you can set the dataType option to "text" in a $.ajax request and parse on the browser side:

$.ajax(url, {
    method: 'POST',
    dataType: 'text'
}).done(function(data) {
    //do something
    var array = data.split(',');
});

Upvotes: 2

Ehsan Hafeez
Ehsan Hafeez

Reputation: 658

You can use Jquery Ajax post call to send data to server. Check the code

    $.ajax({
    type: "POST",
    url: "some.php",
    data: { name: "John", location: "Boston" }
   })
   .done(function( msg ) {
   alert( "Data Saved: " + msg );
   });

Upvotes: 0

i100
i100

Reputation: 4666

This is no a valid JSON (you may check it here). {} means an Object and you must separate objects either with comma or semi-column. semi-column means to assign it to different variables; coma means that it is an array

So it should be something like

[{"ID": "VM-WIN7-64","OS": "Windows 7"},
 {"ID": "VM-WIN7-32","OS": "Windows 7"},
 {"ID": "V M-WIN7-32-1","OS": "Windows 7"},
 {"ID": "VM-WIN7-32-2","OS": "Windows 8"}
]

I made it as an array because you're using data.forEach

Upvotes: 1

Rodrigo Polo
Rodrigo Polo

Reputation: 4784

Just send the response as JSON, in express.js is as simple as this:

res.json(object);

Upvotes: 0

Related Questions