Javascript 如何使用Node.js中的jQuery解析来自服务器端的json数据
我正在使用Nodejs和express.js创建一个献血应用程序。下面是我的服务器站点代码Javascript 如何使用Node.js中的jQuery解析来自服务器端的json数据,javascript,jquery,ajax,json,node.js,Javascript,Jquery,Ajax,Json,Node.js,我正在使用Nodejs和express.js创建一个献血应用程序。下面是我的服务器站点代码 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.bo
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) {
}
});
}
函数fetchDoner()
{
var BloodGroup=$(“#dpBloodGroup”).children(“选项”).filter(“:selected”).val();
var City=$(“#dpCity”).children(“选项”).filter(“:selected”).val();
console.log(BloodGroup);
控制台.日志(城市);
$.ajax({
url:“http://127.0.0.1:5000/SearchDoner",
类型:“post”,
数据类型:“json”,
数据:({bg:BloodGroup,c:City}),
跨域:“真”,
成功:功能(结果){
$。每个(结果、函数(索引、obj){
console.log(obj.txtFullname);
$(“#DonerInfo_5”)。追加(“+obj.address+”++“+obj.mno+”)+“对于稳定的应用程序,您需要处理查询结果。如果您有错误,您的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>");
});
......
。。。。。。
$.ajax({
url:“http://127.0.0.1:5000/SearchDoner",
类型:“post”,
数据类型:“json”,
数据:({bg:BloodGroup,c:City}),
跨域:“真”,
成功:功能(结果){
if(result.type==false){
警报(“发生错误:+result.data”);
返回false;
}
$.each(JSON.parse(result.data)、函数(index、obj){
console.log(obj.txtFullname);
$(“#DonerInfo_5”).append(“+obj.address++”+obj.mno++”你能做一个console.log(result)并在这里发布吗?即使在更新代码后我也没有收到任何错误。好的,那么你没有返回结果。如果没有结果,它将不会进入迭代部分。你能发布console.log(rows)的结果吗
在控制器部分?我只需要知道@Huseyin获取的任何行是否是console.log的结果(行):[{“txtFullname”:“ram”,“gender”:“M”,“email”:ram@gmail.com“,”地址“:”A-219,Sec-19,Noida“,”bgp“:”AB+“,”mno“:”7696457545“},{”txtFullname“:”Harinder“,”性别“:”“电子邮件”:abc@gmail.com“,”地址“:”abc nagar“,”bgp“:”AB+“,”mno“:”9643043471“}]在js端,在成功回调中,console.log(result)
的输出是什么?那里什么都没有。。。。