GET Ajax返回响应的html代码而不是json对象 [英] GET Ajax returns html code in response instead of json object

查看:126
本文介绍了GET Ajax返回响应的html代码而不是json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ajax get请求,如下所示。我正在使用nodejs express向openshift中的server.js发出GET请求。但是,我在响应方法中获取html内容而不是json对象。这两个请求都是针对同一个域。我使用的节点模块是moongojs,mongodb和bson。

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

                }
            });

我的server.js文件包含以下代码

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


推荐答案

res.send(姓名) )不是JSON,你必须使用 JSON.stringify 对数据进行字符串化。

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

在DB调用之前尝试测试以检查它是否有效。

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

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

编辑

正如在注释,请检查以确保您的请求被路由到您声明的正确路由。

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

console.log(gat method) ; 将任何内容输出到终端窗口?

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

这篇关于GET Ajax返回响应的html代码而不是json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆