[英]“NetworkError: 404 Not Found” - $.post() with jQuery
這是我們的 Rails 3.2 應用程序中響應以“order_order_items_attributes”開頭的字段更改的 js 代碼:
$(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.location
:
如果我們用 $.get() 替換 $.post(),那么代碼就可以正常工作並在服務器上啟動 ajax 響應:
$.get(window.location, $('form').serialize(), null, "script"); #works!
但是我們必須使用 $.post() 因為大量數據被發布到服務器。 jquery 文檔顯示 $.get() 和 $.post() 具有完全相同的格式。 我們在這里用 $.post() 錯過了什么?
耙路線輸出:
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 中刪除了與工作流相關的操作。
使用 POST HTTP 動詞時,您的后端路由未正確將該 URL 路由到有效控制器。 在終端中 Rails 項目的根目錄中,運行rake routes
以查看所有可用路由以及它們的結束位置。 沒有看到您的routes.rb
我無法准確解釋問題所在,但這絕對是后端路由問題。
我不會推薦某些評論所說的內容,只是“將其粘貼在 routes.rb 中,它會起作用”。 你的路線應該得到很好的維護,並使用正確的路線助手來完成這項工作。 如果你扔雜項。 路由在那里解決問題時,你最終會得到一堆意大利面條作為你的路由,隨着時間的推移,你的應用程序的維護將變得更加困難。
編輯:更新以引用問題中的更新當前頁面 URL 是/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
操作是對orders_path
的POST
,解析為/po/orders/
。 如果您 POST 到此 URL,則一切正常。 如果您希望能夠發布到您當前使用的 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
現在,當你做一個POST到這個網址( /po/orders/new
,它將擊中OrdersController
create
方法。您仍然可以通過張貼打這個方法/po/orders
,以及(正如我上面推薦)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.