通过节点/反应中的“新请求”,如何通过GET请求传递参数?
我在我的reactjs应用程序中有以下的API调用:
static getAllSkills(title_id) { const request = new Request(`http://localhost:4300/api/v1/job_title_skills`, { method: 'GET', headers: new Headers({ 'Content-Type': 'application/json' }), body: JSON.stringify({title_id: title_id}) }); return fetch(request).then(response => { return response.json(); }).catch(error => { return error; }); }
哪一个指向一个Rails的端点,期望的参数title_id像这样:
def index @skills = Skill.where(id: params[:title_id]) .... end
控制器正在期待一个GET请求但是与上述,我得到以下JS控制台错误:
Uncaught TypeError: Failed to construct 'Request': Request with GET/HEAD method cannot have body.
什么是正确的方式来构buildRequest
并传递给API的参数? 谢谢
我认为你的API中的URL正在等待title_id
也许就像:
api/v1/job_title_skills/:title_id
所以当你提出请求的时候你可以把它附加到你的url中:
static getAllSkills(title_id) { const request = new Request(`http://localhost:4300/api/v1/job_title_skills/${title_id}`, { headers: new Headers({ 'Content-Type': 'application/json' }) }); return fetch(request).then(response => { return response.json(); }).catch(error => { return error; }); }