0

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?)

enter image description here

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:

enter image description here

    $(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);
        }
    });


}
Penguen
  • 16,836
  • 42
  • 130
  • 205

1 Answers1

1

You say jquery that response will be jsonp but it is json. You need to check the difference, i suppose that you should use:

dataType: "json",

jsonp is fo executable functions, see ( What are the differences between JSON and JSONP? )

Community
  • 1
  • 1
MadDocNC
  • 670
  • 5
  • 17
  • What about cross domain? without jsonp, have can i handle cross domain? – Penguen Dec 25 '16 at 16:41
  • jquery-2.0.3.min.js:6 XMLHttpRequest cannot load http://127.0.0.1:5000/Notifies?_=1482684143180. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:13227' is therefore not allowed access. send @ jquery-2.0.3.min.js:6 Test.html:36 error error NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://127.0.0.1:5000/Notifies?_=1482684143180'. – Penguen Dec 25 '16 at 16:43
  • 1
    @Penguen - Do you know what cross-origin security limitations are in a browser? You can't request ajax calls from a site other than the current page's domain unless that site explicitly enables cross origin requests. You can search for "node.js CORS" to find out how to enable cross origin requests in your node.js server. Postman does not restrict cross origin requests like a browser does. – jfriend00 Dec 25 '16 at 20:02