0

jquery ajax 帖子返回404 Not Found in Firebug ...我有什么遗漏吗?

编辑:这是以前工作的,突然停止工作 routes.rb

  #invites
  match '/invites/request' => 'invites#request_invite'

邀请控制器.rb

class InvitesController < ApplicationController

    def request_invite

        if !request.xhr?
            render_404
            return
        end

        email_exists = Invite.where(:email => params[:invite][:email]).exists?

        if email_exists
            @return = { :error => false, :response => "<div class=\"success\">Thank you!</div>" }
        else
            @invitation = Invite.new(params[:invite])

            if @invitation.save
                @return = { :error => false, :response => "<div class=\"success\">Thank you!</div>" }
            else
                error_message = '<div class="error_message">' + @invitation.errors.full_messages.map {|error| "<p>#{error}</p>"}.join + "</div>"
                @return = { :error => true, :response => error_message } 
            end
        end

        render :json => ActiveSupport::JSON.encode( @return )

    end

end

邀请.rb

class Invite < ActiveRecord::Base

    validates :email, :presence => true, :uniqueness => true

    validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

end

javascript:

//post ajax
jQuery.fn.postAjax = function(success_callback) {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), success_callback);
    return false;
  })
  return this;
};
$(document).ready(function() {

  $("#invites_form").postAjax(function(data){
    if(data.error == true){
      popup.init({
        title : "Please fix the following errors:",
        body : data.response
      });
    }
  });

});
4

1 回答 1

0

我已将访问用户页面的方式从 /users/show/:id 更改为 /:id ,这导致了问题

所以我将我的routes.rb文件更改为

  match '/:username/' => 'users#show'
  #invites
  match '/invites/request' => 'invites#request_invite'
  #get user profile pages
  match '/:username/:page' => 'users#show'

现在它工作正常

于 2012-08-16T10:25:56.760 回答