如何使用Node.js中的jQuery解析来自服务器端的json数据

时间:2014-04-11 05:55:06

标签: javascript jquery ajax json node.js

我正在使用Nodejs创建一个献血应用程序,express.js.Below是我的服务器站点代码。

app.post('/SearchDoner', function(request, response) {
  var store;
  console.log('here');
  console.log(request.body);
  console.log(request.body.bg);
  console.log(request.body.c);
  var bg = request.body.bg;
  var city = request.body.c;
  response.setHeader('Content-Type', 'application/json');
  console.log('select txtFullname,gender,email,address,bgp,mno from information   where   bgp =' +"'"+ bg + "'" + "and city =" + "'" + city +"';");
  connection.query('select txtFullname,gender,email,address,bgp,mno from information  where  bgp =' +"'"+ bg + "'" + "and city =" + "'" + city +"';", function(err, rows) {
     response.json(rows);
     console.log(rows);
     });
  });

以下是客户端ajax调用:

 function fetchDoner()
  { 
 var BloodGroup = $("#dpBloodGroup").children("option").filter(":selected").val();
 var City = $("#dpCity").children("option").filter(":selected").val();
 console.log(BloodGroup);
 console.log(City);
     $.ajax({
        url:"http://127.0.0.1:5000/SearchDoner",
        type: "post",
        dataType: 'json',
        data:({bg:BloodGroup,c:City}),    
        crossDomain: "true",      
        success: function (result) {
            $.each(result,function(index,obj){
          console.log(obj.txtFullname);
              $("#DonerInfo_5").append("<tr><td style='color:;background: #FDC68F;''><span>"  + obj.txtFullname + "</span></td>" + "<td style='color:;background: #FDC68F;''><span>"  +  obj.address + "</span></td>" + "<td style='color:;background: #FDC68F;''><span>"  +  obj.gender + "</span></td>" +"<td style='color:;background: #FDC68F;''><span>"  +  obj.mno + "</span></td>" +"<td style='color:;background: #FDC68F;''><span>"  +  obj.email + "</span></td></tr>");
      }); 
  var newUrl = 'http://localhost/Blood%20Donation%20Site_1/donerinfo.html';
  document.location.href = newUrl;
         },error: function (obj, txtStatus, error) {
       }
    });
  }

问题是来自服务器站点的json数据(来自app.post()方法)不会在donerinfo.html页面解析。

1 个答案:

答案 0 :(得分:0)

您需要处理稳定应用程序的查询结果。如果您有错误,您的js无法解析简单的错误字符串。但是,如果您处理它,您可以给用户正确的响应并防止解析不正确的字符串。在下面的代码中,检查响应错误。在JS中,只有成功的结果响应了json迭代;

.....
connection.query('select txtFullname,gender,email,address,bgp,mno from information  where  bgp =' +"'"+ bg + "'" + "and city =" + "'" + city +"';", function(err, rows) {
        if (err) {
            res.json({type: false, data: err});
        } else {
            res.json({type: true, data: JSON.stringify(rows)});
        }
     });
  });
  ....

在你的js;

  ......
  $.ajax({
        url:"http://127.0.0.1:5000/SearchDoner",
        type: "post",
        dataType: 'json',
        data:({bg:BloodGroup,c:City}),    
        crossDomain: "true",      
        success: function (result) {
            if (result.type == false) {
                alert("Error occured:" + result.data);
                return false;
            }
            $.each(JSON.parse(result.data),function(index,obj){
          console.log(obj.txtFullname);
              $("#DonerInfo_5").append("<tr><td style='color:;background: #FDC68F;''><span>"  + obj.txtFullname + "</span></td>" + "<td style='color:;background: #FDC68F;''><span>"  +  obj.address + "</span></td>" + "<td style='color:;background: #FDC68F;''><span>"  +  obj.gender + "</span></td>" +"<td style='color:;background: #FDC68F;''><span>"  +  obj.mno + "</span></td>" +"<td style='color:;background: #FDC68F;''><span>"  +  obj.email + "</span></td></tr>");
      });

......