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);
    });
  };
2
  • 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. Commented Apr 22, 2014 at 1:03
  • btw, if callback takes response as a parameter, you don't have to wrap it in a function, i.e. : success: callback Commented Apr 22, 2014 at 1:21

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

13 Comments

I have removed the dataType from ajax call,but it still didnt work
Changed the answer to point to some issues with your server code.
I tried the stringify, but it didnt work. i still get the html as response
what html do you get? are you certain that your request is routed to the correct route?
I am getting the original html page, which the user uses it for get request
|
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/

1 Comment

I did as you said. I also removed the dataType from ajax call, still it doesnt work
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.