如何将JSON响应从控制器发送到jQuery并进行检查? [英] How to send json response from controller to jQuery and inspect it?
问题描述
我有这个问题:我正在检查控制器中的用户电子邮件,并发送json成功响应(如果已被接受),并添加输入的CSS样式,我还需要防止提交和添加一些消息.
I have this problem : I'm checking user email in controller and sending json successfull response if it is already taken and add css styling of input, also I need to prevent submit and add some message.
这是我的checkemail操作(用于本文- http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery ):
Here is my checkemail action ( used this article - http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery ):
def checkemail
if !User.where('email = ?', params[:email]).empty?
format.jsonr do
render :json => {
:status => :ok,
:message => "Success!",
}.to_json
end
end
end
以及在我的路线上
checkemail GET /checkemail(.:format)
match "/checkemail", to: 'edit_user#checkemail'
和我的jQuery:
$('#user_email').focusout(function() {
$.getJSON('/checkemail', { email: $('#user_email').val() }, function(data) {
$('#user_email').addClass("error");
});
});
我的HTML输入代码:
my HTML input code:
<input id="user_email" name="user[email]" size="30" type="email" value="">
来自mime_type初始化程序文件:
from mime_type initializers file:
Mime::Type.register_alias "application/json", :jsonr, %w( text/x-json )
来自Fiddler:
Request : GET /checkemail?user@mail.com HTTP/2.0
Response(RAW tab) :
Missing template edit_user/checkemail
JSON tab is empty
问题:如何检查是否发送了json请求?如何阻止提交表单并添加一些消息?
推荐答案
为什么format.jsonr
?对我来说似乎是一个错字.
Why format.jsonr
? Seems like a typo to me.
这篇关于如何将JSON响应从控制器发送到jQuery并进行检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!