2017-01-23 25 views
0

我需要通過AJAX調用來實現這一點,其中一個方便的幫助器函數(在服務器上)可以做到這一點,但REST的做法是什麼?這個用例的REST API模式

我在瀏覽器端有一個項目列表(貓),我需要爲所有這些項目填充另一個細節。

input = [list of cat ids] 
output = [list of {}s which has cat ids & age] 

如果我不用關心正確的REST API,我只是聲明瞭一個方法populate_cat_details(),並會通過GET http://example.com?method=populate_cat_details&cat_ids=1,2,3,4返回一個JSON調用它

+0

這是*太廣*。 「_太寬泛,不清楚,不完整或主要以意見爲基礎的問題可能會被[社區擱置](http://stackoverflow.com/help/closed-questions),直到它們得到改善。」see _ [我可以問什麼問題](http://stackoverflow.com/help/on-topic)_ –

回答

1
GET http://www.example.com/cats -> return all cats 
GET http://www.example.com/cats/:id -> return 1 cat by id 
GET http://www.example.com/cats?id=x&id=y&id=z -> return n cats matching ids 
GET http://www.example.com/cats?age=2&age=3&age=4 -> return n cats matching ages 
GET http://www.example.com/cats?id=x&id=y&id=z&age=2&age=3&age=4 -> return n cats matching ids and ages 

您有其他的方式,如果傳遞參數,如昏迷分隔值例如:IDS = 1,2,3

看看這裏: http://www.restapitutorial.com/lessons/restfulresourcenaming.html

+0

因此,這個想法是,獲取輸入列表的貓的全部細節,這不僅限於年齡,因爲它包括所有其他屬性。 從某種意義上說,我認爲這是一個很好的答案。儘管它將不需要的細節傳輸到客戶端/瀏覽器。 – nehemiah