我试图通过jQuery向Rails控制器发送帖子请求这是帖子请求:
$(document).ready(
function() {
$("#LogIn").submit(function() {
var myemail = $('input[name=Email]').val();
var mypassword = $('input[name=Password]').val();
var myObj = {
"user": {
"email": myemail,
"password": mypassword
}
};
$.ajax({
url: "localhost:3000/user/HendelLogIn",
type: "POST",
data: myObj,
dataType: "application/json"
success: function(data) {
alert(data);
}
});
});
}
);
</script>
你可以看到post请求到以下地址:localhost:3000 / user / HendelLogIn
这是HendelLogIn方法:
def HendelLogIn
email = params[:user][:email]
password = params[:user][:password]
@User = User.authenticate(email, password)
if @User
redirect_to :controller => "user", :action => "profile", :id => @User.id
end
正如您可以看到此操作重定向到以下操作:
def profile
@User = User.find(params[:id])
if @User
respond_to do |format|
format.json {render :json => @User }
end
end
end
渲染Json对jQuery帖子的响应,但是在这个过程中我在rails控制台中遇到以下错误:
Redirected to localhost:3000/user/1/profile
Completed 302 Found in 140ms (ActiveRecord: 1.0ms)
[2014-09-02 14:12:03] ERROR Errno::ECONNABORTED: An established connection was aborted by the software in your
主机。
c:/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:396:in `write'
c:/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:396:in `<<'
c:/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:396:in `_write_data'
c:/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:368:in `send_body_string'
c:/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:249:in `send_body'
c:/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:152:in `send_response'
c:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:110:in `run'
c:/Ruby193/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
答案 0 :(得分:0)
您的javascript和样式表标签可能存在问题。
从application.html.erb中的标题部分删除所有标记。
希望这样可以解决您的问题。