0
如何將JSON對象從XMLHttpRequest傳遞到我的jade文件?只顯示一個空白頁面,並且服務器爲get方法發送500內部錯誤。在node.js中使用外部json加入jade
var express = require('express');
var router = express.Router();
var XMLHttpRequest = require("XMLHttpRequest").XMLHttpRequest;
/* GET home page. */
router.get('/:id', function (req, res) {
var id = req.params.id;
getJSON('/pros/' + id, function (data) {
res.render('consultProfessionnal', { prof:JSON.stringify(data)});
}, function (status) {
alert('Something went wrong.');
});
});
module.exports = router;
var getJSON = function (url, successHandler, errorHandler) {
xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
var status = xhr.status;
if (request.readyState === 4 && status === 200) {
successHandler && successHandler(xhr.response);
} else {
errorHandler && errorHandler(status);
}
};
xhr.send();
};
這裏是我的玉文件,其中我發送JSON對象:
extends layout
block content
.container()
#viewItem
if prof.sexe==1
a
img.img-responsive(src='https://cdn2.iconfinder.com/data/icons/ios-7-icons/50/user_male2-128.png', alt='', style='padding-left:60px;')
else if prof.sexe==0
a
img.img-responsive(src='https://cdn0.iconfinder.com/data/icons/futurama/128/Brainslug.png', alt='', style='padding-left:60px;')
else
a
img.img-responsive(src='https://cdn2.iconfinder.com/data/icons/ios-7-icons/50/user_female2-128.png', alt='', style='padding-left:60px;')
h2= prof.prenom + ' ' + prof.nom
h4.space= 'Speciality : ' + prof.specialite
if prof.sexe==1
h4= 'Sexe : Male'
else if prof.sexe==0
h4= 'Sexe : Other'
else
h4= 'Sexe : Female'
h4= 'Total visit : ' + prof.nbreTotal
h4= 'Total patient : ' + prof.nbrePatient
hr
h3 2014 Patient(s)
hr
each visite in prof.patient2014
li= visite.prenom + ' ' + visite.nom
button(
type='submit'
style='width: 25px; height: 25px; background-color: black;'
id=prof._id
onclick="alerted()"
)
script(src='/stylesheets/js/jquery.js')
script(src="../javascripts/script.js")
script(src='/stylesheets/js/bootstrap.min.js')
你會發布你的jade文件嗎? – mgwhitfield 2014-11-24 21:28:49
您正在以尷尬的方式混合客戶端和服務器代碼。代碼在哪裏運行? – generalhenry 2014-11-24 21:29:29
我剛剛添加了翡翠,並從一個快速應用程序運行,我必須打電話給我的REST服務在翡翠網站上打印我的數據 – Jaythaking 2014-11-24 21:38:34