我正在尝试使用Express和Request调用College Score Card API。当我搜索某个特定的学校时,我会收到几所学校的成绩,但不是我搜索过的学校。这是我的代码的一部分:
var fields = '_fields=school.name,2013.aid.median_debt.completers.overall,2013.repayment.1_yr_repayment.completers,2013.earnings.10_yrs_after_entry.working_not_enrolled.mean_earnings&page=100';
var requestUrl = 'https://api.data.gov/ed/collegescorecard/v1/schools.json?api_key=' + apiKey + '&' + fields;
module.exports = function(app) {
app.get('/school', function(req, res, next) {
request(requestUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
console.log(json);
} else {
console.log("There was an error: ") + response.statusCode;
console.log(body);
}
});
})
};
HTML:
<form action="/school" method="GET">
<input type="text" class="form-control" name="school_name" value="" id="enter_text">
<button type="submit" class="btn btn-primary" id="text-enter- button">Submit</button>
</form>
答案 0 :(得分:0)
您需要将学校名称合并到网址中。从为method=GET
设置的表单中,名称将显示在req.query.school_name
中。因此,您不必将请求发送至requestUrl
,而是将其发送至:
requestUrl + "&school_name=" + req.query.school_name
将把它添加到URL的末尾:
&school_name=Pepperdine
或者,放入您的代码,它看起来像这样:
module.exports = function(app) {
app.get('/school', function(req, res, next) {
request(requestUrl + "&school_name=" + req.query.school_name, function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
console.log(json);
res.send(...); // send some response here
} else {
console.log("There was an error: ") + response.statusCode;
console.log(body);
res.send(...) // send some response here
}
});
})
};