express.js 4 JSON 解析来自 GET 请求的问题(从数据表请求数据)

Posted

技术标签:

【中文标题】express.js 4 JSON 解析来自 GET 请求的问题(从数据表请求数据)【英文标题】:express.js 4 JSON parsing problems from GET request (request data from datatables) 【发布时间】:2015-02-03 21:28:52 【问题描述】:

我在解析发送到我的 express.js 4 服务器的序列化 JSON Ajax Get 请求(使用 jQuery $.ajax)时遇到问题。作为 JSON 请求发送的数据由数据表生成。

这是我从客户端开始的方式

$(document).ready(function() 
    $('#example').dataTable( 
        "bServerSide": true,
        "fnServerData": function (sSource, aoData, fnCallback) 
            $.ajax(
                "dataType": 'json',
                contentType: 'application/json',
                "type": "GET",
                "url": "http://localhost:3000/ajax/phenotypes/withOrg/like/datatables/",
                "data": aoData,
                "success": fnCallback,
                "error": function () 
                    alert('have some problem');
                
            );
                           
     );
 );

当我在浏览器数据表中加载此代码时,会生成以下 GET 请求 URL(到服务器):

获取 /ajax/phenotypes/withOrg/like/datatables/?draw=1&columns=%5Bobject+Object%5D%2C%5Bobject+Object%5D%2C%5Bobject+Object%5D%2C%5Bobject+Object%5D%2C%5Bobject +Object%5D%2C%5Bobject+Object%5D&order=%5Bobject+Object%5D&start=0&length=10&search=%5Bobject+Object%5D

或解码形式(firebug 输出)

 columns    [object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
    draw    1
    length  10
    order   [object Object]
    search  [object Object]
    start   0

所以我在发送之前序列化了数据

$(document).ready(function() 
            $('#example').dataTable( 
                "bServerSide": true,
                "fnServerData": function (sSource, aoData, fnCallback) 
                    var myData = JSON.stringify(aoData);
                    $.ajax(
                        "dataType": 'json',
                        contentType: 'application/json',
                        "type": "GET",
                        "url": "http://localhost:3000/ajax/phenotypes/withOrg/like/datatables/",
                        "data": myData,
                        "success": fnCallback,
                        "error": function () 
                            alert('have some problem');
                        
                    );
                                   
             );
         );

这是从数据表生成的 GET 参数:

获取 /ajax/phenotypes/withOrg/like/datatables/?[%22name%22:%22draw%22,%22value%22:1,%22name%22:%22columns%22,%22value%22:[ %22data%22:0,%22name%22:%22%22,%22searchable%22:true,%22orderable%22:true,%22search%22:%22value%22:%22%22,%22regex% 22:false,%22data%22:1,%22name%22:%22%22,%22searchable%22:true,%22orderable%22:true,%22search%22:%22value%22:% 22%22,%22regex%22:false,%22data%22:2,%22name%22:%22%22,%22searchable%22:true,%22orderable%22:true,%22search%22: %22value%22:%22%22,%22regex%22:false,%22data%22:3,%22name%22:%22%22,%22searchable%22:true,%22orderable%22: true,%22search%22:%22value%22:%22%22,%22regex%22:false,%22data%22:4,%22name%22:%22%22,%22searchable%22: true,%22orderable%22:true,%22search%22:%22value%22:%22%22,%22regex%22:false,%22data%22:5,%22name%22:%22% 22,%22searchable%22:true,%22orderable%22:true,%22search%22:%22value%22:%22%22,%22regex%22:false],%22name%22:% 22order%22,%22value%22:[%22column%22:0,%22dir%22:%22asc%22],%22name%22:%22start%22,%22value%2 2:0,%22name%22:%22length%22,%22value%22:10,%22name%22:%22search%22,%22value%22:%22value%22:%22%22 ,%22regex%22:false] HTTP/1.1

解码形式(firebug 输出并使用在线工具美化 - 使用 jslint 检查,似乎正确!)

[
   
      "name":"draw",
      "value":1
   ,
   
      "name":"columns",
      "value":[
         
            "data":0,
            "name":"",
            "searchable":true,
            "orderable":true,
            "search":
               "value":"",
               "regex":false
            
         ,
         
            "data":1,
            "name":"",
            "searchable":true,
            "orderable":true,
            "search":
               "value":"",
               "regex":false
            
         ,
         
            "data":2,
            "name":"",
            "searchable":true,
            "orderable":true,
            "search":
               "value":"",
               "regex":false
            
         ,
         
            "data":3,
            "name":"",
            "searchable":true,
            "orderable":true,
            "search":
               "value":"",
               "regex":false
            
         ,
         
            "data":4,
            "name":"",
            "searchable":true,
            "orderable":true,
            "search":
               "value":"",
               "regex":false
            
         ,
         
            "data":5,
            "name":"",
            "searchable":true,
            "orderable":true,
            "search":
               "value":"",
               "regex":false
            
         
      ]
   ,
   
      "name":"order",
      "value":[
         
            "column":0,
            "dir":"asc"
         
      ]
   ,
   
      "name":"start",
      "value":0
   ,
   
      "name":"length",
      "value":10
   ,
   
      "name":"search",
      "value":
         "value":"",
         "regex":false
      
   
]

现在的问题是无法在 express 4 服务器端解析这个字符串化的 URL 我为此使用 express4 req.query 和 url.parse 方法:http://expressjs.com/api.html#req.query 然后尝试使用 JSON.parse() 方法解析接收到的 json 字符串

...
var url = require('url');
...

 router.get('/withOrg/like/datatables/', function (req, res) 
    console.log('getting json string via req.query');
    console.log(req.query);
    console.log('output parsed json object using JSON.parse');
    console.log(JSON.parse(req.query));

    //another try
    console.log('for stack overflows test 2');
    console.log(url.parse(req.url, true).query);
    console.log('output parsed json object using JSON.parse');
    console.log(url.parse(req.url, true).query);
)

两个 json 字符串输出结果都是无效的 json,如您在此处看到的,无法使用 JSON.parse 解析:

getting json string via req.query

'"data":0,"name":"","searchable":true,"orderable":true,"search":"value":"","re​​gex":false,"data ":1,"name":"","searchable":true,"orderable":true,"search":"value":"","re​​gex":false,"data":2, "name":"","searchable":true,"orderable":true,"search":"value":"","re​​gex":false,"data":3,"name": "","searchable":true,"orderable":true,"search":"value":"","re​​gex":false,"data":4,"name":"","可搜索":true,"orderable":true,"搜索":"value":"","re​​gex":false,"data":5,"name":"","searchable":true ,"orderable":true,"search":"value":"","re​​gex":false': '"column":0,"dir":"asc"': ''

output parsed json object using JSON.parse

getting json string via req.query

'"data":0,"name":"","searchable":true,"orderable":true,"search":"value":"","re​​gex":false,"data ":1,"name":"","searchable":true,"orderable":true,"search":"value":"","re​​gex":false,"data":2, "name":"","searchable":true,"orderable":true,"search":"value":"","re​​gex":false,"data":3,"name": "","searchable":true,"orderable":true,"search":"value":"","re​​gex":false,"data":4,"name":"","可搜索":true,"orderable":true,"搜索":"value":"","re​​gex":false,"data":5,"name":"","searchable":true ,"orderable":true,"search":"value":"","re​​gex":false': '"column":0,"dir":"asc"': ''

output parsed json object using JSON.parse

当我尝试解析 json 字符串时,我从 JSON.parse 得到错误

SyntaxError: Unexpected token o
    at Object.parse (native)
    at module.exports (/Users/xxx/yyy/routes/phenotypesAJAX.js:16:19)
    at Layer.handle [as handle_request] (/Users/xxx/yyy/node_modules/express/lib/router/layer.js:82:5)
    at next (/Users/xxx/yyy/node_modules/express/lib/router/route.js:100:13)

这是 express 4 中的错误吗? 我看不出问题出在哪里。在客户端,序列化的数据表 GET 请求似乎是有效的(使用 JSLint 检查)。在 express4 服务器端,我找不到任何其他方式来以不同的方式解析 GET 请求。

非常感谢您的帮助, 奥利弗

【问题讨论】:

【参考方案1】:

我已经通过在 express github bugtracker 上发布这个问题得到了答案。 学过的知识: 我在想复杂的 express.js 请求方法不是专门为特定的请求格式编写的!他们只是使用未经修改就发送给他们的数据格式!

我最开始使用的方法:

req.query 

[..] 仅适用于标准查询字符串,即键=值对, 您提供的网址不是。[..]

解决我的问题的正确方法是:

  url.parse(req.url).query

返回需要手动解码的 URL 的完整参数字符串:

  obj = JSON.parse(decodeURIComponent(query))

这里是完整的解释: https://github.com/strongloop/express/issues/2460

【讨论】:

以上是关于express.js 4 JSON 解析来自 GET 请求的问题(从数据表请求数据)的主要内容,如果未能解决你的问题,请参考以下文章

在“...ge-2.2.1.tgz”,“engin”附近解析时出现错误的 JSON 输入意外结束

如何解决“org.json.simple.JSONObject 无法解析”?

Node.js JSON.stringify() 在输出中导致“。无法使用 Jquery 解析

从文件 express js 提供 json

???? node ? express.js ??? json ?????????

Express.js 服务器端渲染 - 请求 '/json/version/