通过Ajax读取JSON但没有成功

时间:2015-05-04 16:05:16

标签: javascript jquery node.js

我有一个Node JS服务器,我使用以下代码从MySQL数据库发送JSON数据:

app.get('/whatever/api/:IDmut', function(req, res) {
  var aquery = 'select * from mutations where id="' + req.param("IDmut") + '"';
  console.log(aquery);
  pool.query(aquery, function select(error, results) {
    res.send(JSON.stringify(results[0]));
  });
});

这个JSON看起来像这样:

{"id":"SL2.50ch02_51010759_11","chr":"SL2.50ch02","pos":51010759,"ref":"T","ale":"A","g001":"0/0:39:99","g002":"0/0:34:99","g003":"0/0:33:99","g004":"0/0:37:99","g005":"0/0:33:99","g006":"0/0:35:99","g007":"0/0:27:99","g008":"0/0:33:99","g011":"0/0:35:99","g012":"0/0:22:85","g013":"0/0:34:99","g014":"0/0:32:99","g015":"0/0:39:99","g016":"0/0:31:99","g017":"0/0:47:99","g018":"0/0:36:99","g019":"0/0:26:97","g020":"0/0:33:99","g021":"0/0:25:94","g022":"0/0:28:99","g023":"0/0:35:99","g024":"0/0:38:99","g025":"0/0:13:58","g026":"0/0:35:99","g027":"0/0:41:99","g028":"0/0:34:99","g029":"0/0:37:99","g030":"0/0:24:91","g031":"0/0:46:99","g032":"0/0:42:99","g033":"0/0:35:99","g034":"0/0:34:99","g035":"0/0:42:99","g036":"0/0:41:99","g037":"0/0:21:82","g038":"0/0:34:99","g039":"0/0:36:99","g040":"0/0:31:99","g041":"0/0:31:99","g042":"0/0:34:99","g043":"0/0:34:99","g044":"0/0:42:99","g045":"0/0:34:99","g046":"0/0:24:91","g047":"0/0:40:99","g049":"0/0:32:99","g051":"0/0:20:79","g052":"0/0:34:99","g053":"0/0:40:99","g054":"0/0:51:99","g055":"0/0:41:99","g056":"0/0:33:99","g057":"0/0:28:99","g058":"0/0:21:82","g059":"0/0:37:99","g060":"0/0:29:99","g063":"0/0:36:99","g064":"0/0:25:94","g065":"0/0:26:97","g066":"0/0:17:70","g067":"0/0:27:99","g068":"0/0:35:99","g069":"0/0:36:99","g070":"0/0:28:99","g071":"0/0:30:99","g072":"0/0:14:61","g073":"0/1:17:99","g074":"0/0:17:70","g077":"0/0:29:99","g078":"0/0:42:99","g088":"0/0:18:73","g089":"0/0:26:97","g090":"0/0:40:99","g091":"0/0:29:99","g093":"0/0:37:99","g094":"0/0:26:97","g096":"0/0:28:99","g097":"0/0:41:99","g102":"0/0:33:99","g103":"0/0:19:76","g104":"0/0:19:76","g105":"0/0:27:99"}

在clident方面,我有以下代码:

$.ajax({
    type:     "GET",
    url:      "the corresponding url... it is provate so...",
    dataType: "json",
    success: function(data){alert(data);}
});

这不起作用。随处都没有警报。但是,如果我将网址更改为http://echo.jsontest.com/key/value/anotherKey/anotherValue,则可以...

编辑:我刚刚在控制台上看到此错误:

Failed to load resource: the server responded with a status of 401 (Unauthorized)

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'externalfile://' is therefore not allowed access. The response had HTTP status code 401.

3 个答案:

答案 0 :(得分:1)

根据您收到的错误,它听起来像是CORS问题(cross origin resource sharing)。您正在尝试向其他服务器发出HTTP请求,并且服务器拒绝它,因为您没有正确的访问控制标头设置。

This是一个很好的Node和Express库,它提供了添加标题的中间件。

基本上归结为

Access-Control-Allow-Origin : *

您的回复标题。此标头将允许其他域向您的Web服务器发出请求。您可以设置允许发出请求的特定白名单URL或仅允许任何域向您的服务器发出请求的*。

答案 1 :(得分:0)

试试这个(注意:唯一的区别是dataType不是" json",但" jsonp")

$.ajax({
    type:     "GET",
    url:      "the corresponding url... it is provate so...",
    dataType: "jsonp",
    success: function(data){alert(data);},
    error: function(err) {alert(err);}
});

答案 2 :(得分:-3)

也许您应该使用res.json?