1

我想使用Magic Suggest在我的rails應用程序中使用自動完成工作。魔術建議返回郵政404錯誤

我認爲這是一個正確的問題:我如何讓MagicSuggest抓住我給它的URL的JSON?

這是錯誤控制檯將返回當我鍵入字母:

POST http://localhost:3000/search_foods 404 (Not Found) jquery.js:8706 
Uncaught Could not reach server 

這裏是神奇的建議代碼:

input.magicSuggest({ 
     data: "/foods/search/", 
     placeholder: "Search Foods...", 
     valueField:'idFood', 
     displayField:'foodName' 
     });  

路線

resources :search_foods 

控制器和動作

class SearchFoodsController < ApplicationController 
    def index 
    render json: %['Crack', 'Cocain', 'Gorilla Test', 'Horse Test'] 
    end 
end 

當我訪問/ search_foods URL直接我得到

'Crack', 'Cocain', 'Gorilla Test', 'Horse Test' 

因爲我的代碼是專門做。

我認爲這個問題是在MagicSuggest,默認情況下,發送POST請求,雖然我不知道這是完全相關:

You can pass the url from which the component will fetch its JSON data.Data will be fetched 
     *  using a POST ajax request that will * include the entered text as 'query' parameter. The results 
     *  fetched from the server can be: 
     *  - an array of JSON objects (ex: [{id:...,name:...},{...}]) 
     *  - a string containing an array of JSON objects ready to be parsed (ex: "[{id:...,name:...},{...}]") 
     *  - a JSON object whose data will be contained in the results property 
     *  (ex: {results: [{id:...,name:...},{...}] 
+0

whay port no。 3000? –

+0

它的意義是什麼?大聲笑我不知道,否則。這是我的機器上的默認設置。 –

+0

當你說你直接訪問url時,你是否暗示你在瀏覽器中輸入http:// localhost:3000/search_foods並得到結果? –

回答

0

我對POST請求的懷疑是正確的。

我的朋友幫了忙,所以我才解決了這個問題。

這是我做過什麼..

  1. 消除食品搜索控制器,因爲這是沒有必要的。

  2. 創建一個搜索行動在我的飲食控制,像這樣:

    高清搜索 渲染JSON:「可卡因」,「裂縫」,「大猩猩睾酮」] 結束

  3. 編輯我的路線一個POST請求,而非get *這是關鍵:

    資源:食品做 集合做 後:搜索 結束 結束

---另一種選擇,如karlipoppins所暗示的,是簡單地改變的請求magicSuggest使得通過包括該方法的類型屬性,像這樣:

 input.magicSuggest({ 
      method: 'get', 
      data: "/foods/search/", 
      placeholder: "Search Foods...", 
      valueField:'idFood', 
      displayField:'foodName' 
     }); 

然後我將不需要更改發佈的路線。

  1. 添加了此路徑的數據屬性在js

    數據:「/食品/搜索/」

這將是任何人試圖讓magicSuggest到一個巨大的幫助在鐵軌工作。說實話,這是一個非常簡單的設置。這一點和JSON格式是唯一讓我絆倒在這裏的東西。

+0

我很高興你發現你的問題,但它與magicsuggest無關。因此,只是從軌道控制器輸出json的標準方式,並且在您執行任何ajax任何時候都可以使用。只要格式正確,Magicsuggest並不關心格式的格式。如果您希望執行get請求而不是post後,您可以在創建組件時將屬性'method'設置爲'get'。 – karlipoppins

+0

啊..我明白了。我正在尋找一種方法來改變magicSuggest對get請求的請求類型。你只是指出我需要尋找'方法'屬性..我不知道爲什麼我沒有找到這個名字的財產。無論如何,謝謝。 –

1

試試這個:

input.magicSuggest({ 
     data: "http://localhost:3000/search_foods", 
     placeholder: "Search Foods...", 
     valueField:'idFood', 
     displayField:'foodName' 
}); 
+0

與以前相同的確切錯誤。除了URL前面有http:// –

+0

POST http:// localhost:3000/search_foods 404(Not Found)沒有空格(SO刪除http://部分) –

+0

請檢查你的服務器/控制器代碼有一個映射來處理對http:// localhost:3000/search_foods的POST調用。此外,您的響應不是MagicSuggest要求的JSON格式。 –

0

的doc指出組件預計以下某項:

 *  - an array of JSON objects (ex: [{id:...,name:...},{...}]) 
    *  - a string containing an array of JSON objects ready to be parsed (ex: "[{id:...,name:...},{...}]") 
    *  - a JSON object whose data will be contained in the results property 
    *  (ex: {results: [{id:...,name:...},{...}] 

當您訪問/search_foods時,您g et

'Crack', 'Cocain', 'Gorilla Test', 'Horse Test' 

這不適合3種支持的情況。