Rails中如何响应AJAX表单提交?
这是我用 HTML 制作的一个简单的“告诉朋友”表单,它正确地将数据发布回我的服务器并正确提交。
这是 javascript 代码(在 CoffeeScript 中):
$('#tell-a-friend-form').submit (e) ->
e.preventDefault()
console.log "submitted form."
$.ajax
url: 'home/tellafriend'
type: 'POST'
data:
name: $(this).find("input[name='name']").val()
emails: $(this).find("input[name='emails']").val()
message: $(this).find("textarea[name='message']").val()
success: (result) ->
alert("ok")
$('#tell-a-friend-form').find(".loading-icon").hide()
error: (result) ->
alert("Sorry, we couldn't send your message.")
$('#tell-a-friend-form').find(".loading-icon").hide()
$('.tell-a-friend-submit').removeAttr("disabled")
$(this).find(".tell-a-friend-submit").attr("disabled", "disabled")
$(this).find(".loading-icon").show()
现在,在控制器中,我根本不需要返回任何消息,除了“发送成功”类型的布尔值,以便客户端 javascript 可以知道是否“成功”或“错误”。
目前,客户端每次都会触发“错误”。
这是我的控制器代码:
def tellafriend
#send the actual email message. to be implemented.
respond_to do |format|
format.json { render :json => "success" }
end
end
我在控制器方面做错了什么吗?我确信问题就出在这里。如果我想要触发“成功”位,我需要返回什么?
Here is a simple Tell a Friend form I made in HTML, it's correctly POSTING the data back to my server and submitting correctly.
Here's the javascript code (it's in CoffeeScript):
$('#tell-a-friend-form').submit (e) ->
e.preventDefault()
console.log "submitted form."
$.ajax
url: 'home/tellafriend'
type: 'POST'
data:
name: $(this).find("input[name='name']").val()
emails: $(this).find("input[name='emails']").val()
message: $(this).find("textarea[name='message']").val()
success: (result) ->
alert("ok")
$('#tell-a-friend-form').find(".loading-icon").hide()
error: (result) ->
alert("Sorry, we couldn't send your message.")
$('#tell-a-friend-form').find(".loading-icon").hide()
$('.tell-a-friend-submit').removeAttr("disabled")
$(this).find(".tell-a-friend-submit").attr("disabled", "disabled")
$(this).find(".loading-icon").show()
Now, in the Controller, I don't need to return any message at all, except maybe a "sent ok" type of boolean so the client side javascript can know whether to "success" or to "error".
Currently, the client side fires "error" every time.
Here's my Controller code:
def tellafriend
#send the actual email message. to be implemented.
respond_to do |format|
format.json { render :json => "success" }
end
end
Anything I'm doing wrong on the controller side? I'm sure the problem lies here. What do I need to return in case I want the "success" bit to fire?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您发出请求时,您的日志会显示什么?这对于诊断此类事情很重要。我能想到的三件事通常可以解决这个问题:
显式请求 JSON
它可能就像从 AJAX 调用中显式请求 JSON 一样简单:
检查您的路线:
您的路线可能未配置为响应 POST 请求。确保您的“tellafriend”路线接受帖子。作为快速检查,您可以将 AJAX 请求更改为“GET”或访问 /home/tellafriend.json
返回有效对象
而不是简单地返回“成功”您可能想尝试返回实际对象:
What do your logs show you when you make the request? This is important for diagnosing this sort of thing. There are three things I can think of that usually fix this:
Explicitly Request JSON
It might be as simple as explicitly requesting JSON from your AJAX call:
Check Your Routes:
Your route might not be configured to respond to POST requests. Ensure your route for 'tellafriend' accepts post. As a quick check you could change your AJAX request to 'GET' or visting /home/tellafriend.json
Return a Valid Object
Instead of simply returning "success" You might want to try returning an actual object: