Jquery ajax和控制器方法

我有这个控制器方法:

def create render plain: params[:article].inspect #@article = Article.new(article_params) #@article = Article.create(article_params) #if @article.save # redirect_to @article #else # render 'new' #end end 

而这个jquery ajax请求:

 function ajaxsend() { $.ajax({ url: "/articles", type: "POST", data: { "article": { "title": "acme", "text": "text of the article" } }, success: function(resp){ } }); } 

但是在我点击运行e jquery ajax的按钮后,我什么都没得到。 我希望渲染但没有任何反应,甚至在控制台中都没有错误。 我究竟做错了什么? 我不明白如何将数据jquery ajax发送到控制器。 有帮助吗? 我是ruby on rails开发的新手。 Ruby版本:2.1.2; Rails版本:4.1.5;

EDIT1:

当我以传统方式在表单中执行时,它运行良好:

在此处输入图像描述

结果:

在此处输入图像描述

EDIT2:

当我按下按钮时,WEBrick服务器打印出来:

 Started POST "/articles" for 127.0.0.1 at 2014-09-14 09:43:36 +0100 Processing by ArticlesController#create as */* Parameters: {"article"=>{"title"=>"acme", "text"=>"123 Carrot Street"}} Rendered text template (0.0ms) Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms) 

好像没问题。 为什么浏览器没有任何反应? 为什么create方法中的渲染不起作用? 有帮助吗?

填写表单并按Create Article时的WEBrick响应:

 Started POST "/articles" for 127.0.0.1 at 2014-09-14 09:52:21 +0100 Processing by ArticlesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"o8bzaGFcWGaHFFEbgZsfx5bWiHDAIaX3j4xXh3XkTwc=", "article"=>{"title"=>"mmmmm", "text"=>"mmmmm"}, "commit"=>"Create Article"} Rendered text template (0.0ms) Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms) 

EDIT3:

我也尝试过这种方式:

 $.post("/articles", { "article": { "title": "Title1Ar", "text": "text of the article" } }, alert("callback"), "json").error(alert("error handler")); 

这在WEBrick中:

 Started POST "/articles" for 127.0.0.1 at 2014-09-15 09:19:49 +0100 Processing by ArticlesController#create as JSON Parameters: {"article"=>{"title"=>"firsttitle", "text"=>"text of the article"}} Rendered text template (0.0ms) Completed 200 OK in 1ms (Views: 0.5ms | ActiveRecord: 0.0ms) 

但是浏览器中没有任何反应。 有帮助吗?

我这样做ajax调用,你可以尝试一下:

 $("#test-ajax-controller").on("click", function() { request = void 0; request = $.ajax({ url: "/articles/create/?title=acme&text=123 Carrot Street" }); request.done(function(data, textStatus, jqXHR) { console.log(data); }); request.error(function(jqXHR, textStatus, errorThrown) { alert("AJAX Error:" + textStatus); }); }); 

在您的控制器中,您应该使用JSON进行响应

 respond_to do |format| format.json { render json: @your_return_object } end 

好像没问题。 为什么浏览器没有任何反应?

因为您尚未创建任何处理响应的function。

无论何时发送Ajax请求,您都会收到响应。 编写javascript时需要考虑此响应。 我将详细介绍一个针对您的即时修复,然后是一个更系统的修复程序,它将为您提供更好的路径:


阿贾克斯

根据您当前的设置,您将希望使用以下内容接收数据:

 #app/assets/javascripts/application.js function ajaxsend() { $.ajax({ url: "/articles", type: "POST", data: { "article": { "title": "acme", "text": "123 Carrot Street" } }, success: function(resp){ alert("Data"); } }); } 

此实例中的“警报”将创建您需要的效果。 为了使这项工作正常,你将不得不使用自己的代码(我不知道你想要实现的目标)。

还必须注意,您必须将此ajax代码与JS中的事件绑定:

 #app/assets/javascripts/application.js $(document).on("submit", "form", function() { $.ajax({ url: $(this).attr("action"), type: "POST", data: $(this).serialize(), success: function(resp){ alert("Data"); } }); }); 

UJS

使用Rails,我们非常幸运拥有Rails UJS驱动程序,它为我们的Rails应用程序提供了一系列function,我们可以隐式调用它(IE无需继续定义函数)。

您可以在此处查看Rails UJS Ajaxfunction

具体来说,您将能够这样做:

 #app/views/articles/new.html.erb <%= form_for @article, remote: true do |f| %> #app/controllers/articles_controller.rb class ArticlesController < ApplicationController respond_to :js, :json, :html def create @article = Article.new article_params @article.save respond_with @article end private def article_params params.require(:article).permit(:title, :text) end end #app/assets/javascripts/application.js $(document).on("ajax:success", "form", function(status, data, xhr){ alert("DATA RECEIVED"); });