내가 코멘트에 모든 것을 밀어 넣으려고했기 때문에 나는 단지 답할 것입니다.
post '/participants/create', :provides => :json do
# I'd use a 201 as the status if actually creating something,
# 200 while testing.
# I'd send the JSON back as a confirmation too, hence the
# :provides => :json
data = JSON.parse params
# do something with the data, then…
halt 200, data.to_json
# halt because there's no need to render anything
# and it's convenient for setting the status too
end
javascript:
$('#contact').on('submit',function (event) {
event.preventDefault();
$.ajax({
url: 'participants/create',
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data : JSON.stringify({ name: "Dom"}),
accepts: "application/json",
success: function(json) {
alert(json);
}
})
})
전체적으로 왜 JSON을 HTTP 서버로 보냅니 까? 나는 항상 서버에 HTTP, JSON을 자바 스크립트로 보내는 것이 가장 효과적이라는 것을 발견했다. YMMV.
post '/participants/create', :provides => :json do
# Do something with the params, then…
halt 200, params.to_json
end
javascript:
$('#contact').on('submit',function (event) {
event.preventDefault();
$.ajax({
url: 'participants/create',
dataType: 'json',
type: 'POST',
data : { name: "Dom"}, // or $(event.target).serialize()
accepts: "application/json",
success: function(json) {
alert(json);
}
})
})
양식 제출을 막았습니까? – Musa
Hmmmmm ... 좋은 질문입니다. preventDefaults를 사용해야합니까? –
당신의 핸들러 몸체에'p params'를 시도하십시오. Sinatra는 가끔 이상한 param 처리를합니다. – AlexQueue