" NetworkError:404 Not Found" - 带有jquery的$ .post()

时间:2015-02-12 01:01:46

标签: javascript jquery ajax ruby-on-rails-3

以下是我们的Rais 3.2应用中的js代码,用于响应ids开头的字段更改' order_order_items_attributes':

$(function (){
    $(document).on('change', "[id^='order_order_items_attributes'][id$='_name']", function (){
    $.post(window.location, $('form').serialize(), null, "script");
    return false;
  });
});

$ .post()会导致错误:

"NetworkError: 404 Not Found  - http://localhost:3000/po/orders/new?parent_record_id=4&parent_resource=ext_construction_projectx%2Fprojects&project_id=4%22"

以下是window.locationenter image description here

如果我们用$ .get()替换$ .post(),那么代码工作正常并激活服务器上的ajax响应:

$.get(window.location, $('form').serialize(), null, "script");  #works!

但是我们必须使用$ .post(),因为大量数据被发布到服务器。 jquery文档显示$ .get()和$ .post()具有完全相同的格式。我们在这里错过了$ .post()?感谢。

更新:rake路线输出:

Routes for PurchaseOrderx::Engine:
        search_order_items GET    /order_items/search(.:format)         purchase_orderx/order_items#search
search_results_order_items GET    /order_items/search_results(.:format) purchase_orderx/order_items#search_results
         stats_order_items GET    /order_items/stats(.:format)          purchase_orderx/order_items#stats
 stats_results_order_items GET    /order_items/stats_results(.:format)  purchase_orderx/order_items#stats_results
               order_items GET    /order_items(.:format)                purchase_orderx/order_items#index
                           POST   /order_items(.:format)                purchase_orderx/order_items#create
            new_order_item GET    /order_items/new(.:format)            purchase_orderx/order_items#new
           edit_order_item GET    /order_items/:id/edit(.:format)       purchase_orderx/order_items#edit
                order_item GET    /order_items/:id(.:format)            purchase_orderx/order_items#show
                           PUT    /order_items/:id(.:format)            purchase_orderx/order_items#update
                           DELETE /order_items/:id(.:format)            purchase_orderx/order_items#destroy
             search_orders GET    /orders/search(.:format)              purchase_orderx/orders#search
     search_results_orders GET    /orders/search_results(.:format)      purchase_orderx/orders#search_results
              stats_orders GET    /orders/stats(.:format)               purchase_orderx/orders#stats
      stats_results_orders GET    /orders/stats_results(.:format)       purchase_orderx/orders#stats_results
        event_action_order GET    /orders/:id/event_action(.:format)    purchase_orderx/orders#event_action
        acct_approve_order PUT    /orders/:id/acct_approve(.:format)    purchase_orderx/orders#acct_approve
         acct_reject_order PUT    /orders/:id/acct_reject(.:format)     purchase_orderx/orders#acct_reject
          gm_approve_order PUT    /orders/:id/gm_approve(.:format)      purchase_orderx/orders#gm_approve
           gm_reject_order PUT    /orders/:id/gm_reject(.:format)       purchase_orderx/orders#gm_reject
           gm_rewind_order PUT    /orders/:id/gm_rewind(.:format)       purchase_orderx/orders#gm_rewind
              submit_order PUT    /orders/:id/submit(.:format)          purchase_orderx/orders#submit
  list_open_process_orders GET    /orders/list_open_process(.:format)   purchase_orderx/orders#list_open_process
                    orders GET    /orders(.:format)                     purchase_orderx/orders#index
                           POST   /orders(.:format)                     purchase_orderx/orders#create
                 new_order GET    /orders/new(.:format)                 purchase_orderx/orders#new
                edit_order GET    /orders/:id/edit(.:format)            purchase_orderx/orders#edit
                     order GET    /orders/:id(.:format)                 purchase_orderx/orders#show
                           PUT    /orders/:id(.:format)                 purchase_orderx/orders#update
                           DELETE /orders/:id(.:format)                 purchase_orderx/orders#destroy
                      root        /                                     purchase_orderx/orders#index

以下是采购订单引擎的佣金路线输出。大多数路线与问题无关,仍然按原样列出。

这里是routes.rb:

resources :order_items do
    collection do
      get :search
      get :search_results
      get :stats
      get :stats_results 
    end
  end

  resources :orders do
    collection do
      get :search
      get :search_results
      get :stats
      get :stats_results 
    end

  end

在routes.rb中删除了与工作流程相关的操作,以便于阅读。

1 个答案:

答案 0 :(得分:2)

使用POST HTTP谓词时,您的后端路由未正确将该URL路由到有效的控制器。在终端的Rails项目的根目录中,运行rake routes以查看所有可用路由以及它们最终的位置。如果没有看到您的routes.rb,我无法准确解释错误,但它确实是后端路由问题。

我不会推荐一些评论所说的内容,只是将其粘贴在routes.rb中,它会起作用"。您的路线应妥善维护,并使用正确的路线助手完成工作。如果你扔misc。在那里路线解决问题,你最终会得到一堆意大利面条,随着时间的推移,你的应用程序的维护将变得更加困难。

编辑:已更新以引用问题中的更新 当前页面网址为/po/orders/new。查看您的rake routes输出,这会映射到new_order_path,由此行显示:

new_order GET    /orders/new(.:format)                 purchase_orderx/orders#new

如果您直视它上方,您会看到create操作的真实路线:

POST   /orders(.:format)                     purchase_orderx/orders#create

create操作为POSTorders_path,其解析为/po/orders/。如果你POST到这个URL,一切都应该工作。如果您希望能够发布到目前正在使用的网址并让其发挥作用,只需修改您的routes.rb即可:

resources :order_items do
  collection do
    get :search
    get :search_results
    get :stats
    get :stats_results 
  end
end

resources :orders do
  # Manually route POSTs to /new to the create action
  post "/new", :controller => :orders, :action => :create

  collection do
    get :search
    get :search_results
    get :stats
    get :stats_results 
  end
end

现在,当您对此网址/po/orders/new发帖时,它会点击OrdersController create方法。您仍然可以通过发布到/po/orders来点击此方法(如上所述)。