【问题标题】:How to serve an Ajax post request如何处理 Ajax 发布请求
【发布时间】:2012-07-17 17:23:54
【问题描述】:

这个问题乍一看似乎很简单,但是却拖慢了我的工作!请帮我弄清楚我在哪里犯了错误。

我想使用 Ruby-on-Rails 构建一个服务器,它具有一个主要功能:使用来自我的客户端 JS 的一些数据来提供发布请求。然后使用jquery的ajax方法发送异步请求:

    function processPeople() {
        SOME.api(".get", {owner_id:frnds.response[i].uid,count:"3"}, function(posts) {
                                var usefulPosts = posts.response;
                                for (var i = 1; i <= 2; i++) {

            $.ajax({
                type: 'POST',
                url: 'wallposts',
                data: {uid: usefulPosts[i]["from_id"], uniqpost_id: usefulPosts[i]["id"], tent:usefulPosts[i]["text"]}
            });

    };
    });
    i++;
    if (i<2) {
        setTimeout(processPeople, 1000);
    }
}

这里是发送请求的控制器:

def create
    # debugger

    if !(Wallpost.from_user(params[:uid]).find_by_uniqpost_id(params[:uniqpost_id]))
    @newWP = Wallpost.new(:uid => params[:uid], :content => params[:content], 
            :uniqpost_id => params[:uniqpost_id])
    @newWP.save 


end

如果我只想在服务器端保存数据而没有更多,我会在服务器端做什么?每次我尝试将信息发布到我的服务器时,它都会以 500 错误回复我:

ActionView::MissingTemplate (Missing template wallposts/create, application/create with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/home/yurgen/rails_project/vkraigslist/app/views"

【问题讨论】:

    标签: ruby-on-rails ajax controller


    【解决方案1】:

    您需要指定控制器的返回。 HTTP 协议请求。

    如果您不想返回任何代码,只需输入一个

    head :ok
    

    在你的控制器的末尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 2010-09-06
      相关资源
      最近更新 更多