5

I have an ajax get request as below. I am making a GET request to server.js in openshift using nodejs express. However, i get html contents in the response method instead of json object. Both the requests are for the same domain.The node modules that I am using are moongojs,mongodb and bson.

$.ajax({
           type: "GET",
           url: "http://abc-favspot.rhcloud.com",
           contentType: "application/json",
           data: JSON.stringify(currLocation),
           dataType: "text",
           success: function(response){
           callback(response);
               },
            error: function( error ){
            console.log( "ERROR:", error );

                }
            });

My server.js file has the following code

self.routes['getData'] = function(req, res){
        console.log("gat method");            

    self.db.collection('location').find().toArray(function(err, names) {
       res.header("Content-Type:","application/json");
        console.log("success get");            
        res.send(names);
    });
  };
misthacoder
  • 127
  • 1
  • 1
  • 11
  • Try simplifying the request first. Don't make a db call, simple return some JSON in your `server.js`. Do this to make sure that you are calling the correct route. – Jason J. Nathan Apr 22 '14 at 01:03
  • btw, if `callback` takes response as a parameter, you don't have to wrap it in a function, i.e. : `success: callback` – Jason J. Nathan Apr 22 '14 at 01:21

3 Answers3

2

res.send(names) is not JSON, You have to stringify the data using JSON.stringify.

Try testing before the DB call to check if it works.

res.send( JSON.stringify( {testData:'test'} ) )

Edit

As discussed in the comments, please check to ensure that your request is routed to the correct route that you declared.

Does console.log("gat method"); output anything to the terminal window?

Jason J. Nathan
  • 7,422
  • 2
  • 26
  • 37
  • I have removed the dataType from ajax call,but it still didnt work – misthacoder Apr 22 '14 at 00:59
  • Changed the answer to point to some issues with your server code. – Jason J. Nathan Apr 22 '14 at 01:10
  • I tried the stringify, but it didnt work. i still get the html as response – misthacoder Apr 22 '14 at 01:39
  • what html do you get? are you certain that your request is routed to the correct route? – Jason J. Nathan Apr 22 '14 at 02:42
  • I am getting the original html page, which the user uses it for get request – misthacoder Apr 22 '14 at 02:48
  • Then there must be something wrong with your routing. You need to route to the correct action. From what I see above, `self.routes['getData']` should point to `http://abc-favspot.rhcloud.com/getData`. What router are you using? Does `console.log("gat method");` output anything to the terminal window? – Jason J. Nathan Apr 22 '14 at 02:53
  • thats the thing, it doesnt even go in the self.routes['getData'].Hence no output. – misthacoder Apr 22 '14 at 02:58
  • self.app.get('/', self.routes['getData']); calls self.routes['getData'] – misthacoder Apr 22 '14 at 03:11
  • Could you edit your post to include your node modules you are using? – Jason J. Nathan Apr 22 '14 at 03:24
  • I have updated my post to add node modules. It's in the first paragrapg, last line – misthacoder Apr 22 '14 at 03:59
  • When i do self.app.get('/ws', self.routes['/']); where the ajax request is the same as you suggested, i get the output from self.app.get. But it gets displayed on the webapge, i want this response back in ajax request, so that I can populate in my html page – misthacoder Apr 22 '14 at 04:14
  • Thank you, for pointing out the routing. I fixed it. In url section of the ajax get request I updated it to url: "http://abc-favspot.rhcloud.com/index". and in server.js file I used self.app.get('/index', self.routes['getData']); – misthacoder Apr 22 '14 at 06:11
  • You're welcome, glad to be of help. I'll edit the answer so you can close this question! – Jason J. Nathan Apr 22 '14 at 10:05
1

in your $.ajax method call change this

dataType: "text",

to

dataType: "json",

refer to the documentation for the further details

https://api.jquery.com/jQuery.ajax/

and please check if the data you receive is a valid json, check it with http://jsonlint.com/

Netorica
  • 18,523
  • 17
  • 73
  • 108
0

You can use res.json to send JSON response instead of res.send

res.json(obj)

This method also set Content-Type as application/json

Fizer Khan
  • 88,237
  • 28
  • 143
  • 153