I have been passing lots of time to understand the reason Why my Node.js service is working very well by using postman. If you look below you can see my node.js service is working perfect. But JQUERY code (calling GetAllNotifyTypesFunc();)
Gives me error: (how to call correct both postman and jquery without callbacks?)
Node.js :
'use strict';
var express = require("express");
var app = express();
var MongoClient = require('mongodb').MongoClient;
var router = express.Router();
app.get('/Notifies', function (req, res) {
MongoClient.connect('mongodb://127.0.0.1:27017/Test', function (err, db) {
if (err) throw err;
var coll = db.collection('Notifies');
coll.find({}).toArray(function (err, result) {
if (err) {
res.send(err);
} else {
// res.writeHead(200, {
// 'Content-Type': 'application/json'
// });
// res.end('callback(\'' + JSON.stringify(result) + '\')');
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify(result));
// res.json(result);
}
})
})
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function () {
console.log("Listening on " + port);
})
if I use Postman:
$(function () {
GetAllNotifyTypesFunc();
});
var GetAllNotifyTypesFunc = function () {
console.log("notify");
$.ajax({
url: 'http://127.0.0.1:5000/Notifies',
dataType: "jsonp",
async: false,
//jsonpCallback: "callback",
cache: false,
timeout: 5000,
success: function (data) {
console.log(data);
console.log(JSON.parse(data));
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
}