jQuery Ajax和控制器方法 [英] Jquery ajax and controller method
问题描述
我有这个控制器方法:
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请求:
And this jquery ajax request:
function ajaxsend() {
$.ajax({
url: "/articles",
type: "POST",
data: { "article": { "title": "acme", "text": "text of the article" } },
success: function(resp){ }
});
}
但是在我单击运行jquery ajax的按钮后,我什么也没得到.我希望可以渲染,但是什么也没有发生,甚至控制台中都没有错误. 我究竟做错了什么?我不明白如何将jquery ajax的数据发送到控制器. 有什么帮助吗?我是Rails开发方面的红宝石新手. Ruby版本:2.1.2; Rails版本:4.1.5;
But after I click the button that run e jquery ajax I get nothing. I expect the rendering but nothing happens, not even a error in the console. What am I doing wrong? I don't understand how can I send data thought jquery ajax to the controller. Any help? I'm new in ruby on rails development. Ruby version: 2.1.2; Rails version: 4.1.5;
当我以以下方式以传统方式进行操作时,效果很好:
It works well, when I do it in the traditional way in the form:
结果:
当我按下按钮时,WEBrick服务器将打印以下内容:
The WEBrick server prints this, when I press the button:
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方法中渲染不起作用?有帮助吗?
It seems ok. Why does nothing happens in the browser? Why the render in create method is not working? Any help?
我填写表单并按创建文章时的WEBrick响应:
The WEBrick response when I fill the form and press Create Article:
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中:
This in the 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)
,但是浏览器中什么也没有发生. 有帮助吗?
but again nothing happens in the browser. Any help?
推荐答案
我像这样进行ajax调用,可以尝试一下:
I make ajax call like this, you can try it:
$("#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响应
And in your controller you should respond in JSON
respond_to do |format|
format.json { render json: @your_return_object }
end
这篇关于jQuery Ajax和控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!