GET Ajax returns html code in response instead of json object

I have an ajax get request as shown below. I am making a GET request to server.js in openshift using nodejs express. However, I get html content in the response method instead of the json object. Both requests belong to the same domain. The node modules I use 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 );

                }
            });

In my server.js file there is 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);
    });
  };
+3
source share
3 answers

res.send(names)is not JSON, you must scribble the data with JSON.stringify.

Try testing before calling the database to see if it works.

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

Edit

, , , , .

console.log("gat method"); - ?

+1

$.ajax

dataType: "text",

dataType: "json",

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

, , , json, http://jsonlint.com/

+1

res.json JSON res.send

res.json(obj)

Content-Type application/json

0

All Articles