Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Не получается вывод данных из RESTful api (https://javascript.ru/forum/misc/57704-ne-poluchaetsya-vyvod-dannykh-iz-restful-api.html)

Wahtel 15.08.2015 15:00

Не получается вывод данных из RESTful api
 
У меня очень простое задание, просто вывод салатов с меню ресторана используя готовый api, который мне отправили вместе с заданием называется файл api.raml, делаю я задание на AngularJs, проблема в том что api.raml выглядит вот так:
#%RAML 0.8
title: barashki.net
baseUri: [url]http://online.invensio.com/test/sheeps[/url]
version: v0.01
/index.php:
  displayName: All values data
  post:
    description: Return array of all dishes on site
    responses:
      200:
        body:
          application/json:
            schema: |
              - AllDishesResponse: {"$schema": "http://json-schema.org/schema",
                "type": "array",
                "items": { "$ref": "AllDishesResponseItem" }
              }
              - AllDishesResponseItem: |
                {
                  "type": "object",
                  "properties": {
                    "tid":  { "type": "number" },
                    "vid":  { "type": "number" },
                    "name":  { "type": "string" },
                    "description":  { "type": "string" },
                    "format":  { "type": "string" }
                  }
                }
            example: |
              [
                {
                  "tid": "10",
                  "vid": "7",
                  "name": "Завтраки",
                  "description": "",
                  "format": "filtered_html"
                },
                {
                  "tid": "11",
                  "vid": "7",
                  "name": "Салаты",
                  "description": "",
                  "format": "filtered_html"
                },
                {
                  "tid": "12",
                  "vid": "7",
                  "name": "Супы",
                  "description": "",
                  "format": "filtered_html"
                }         
              ]   
/dishes.php:
  displayName: list of dishes
  post:
    description: Request to the server with dishes category. return the list of dishes in category.
    body:
      application/json:
        schema: |
          - ConsumerRequest: {"$schema": "http://json-schema.org/schema",
            "type": "object",
            "properties": {
              "id":  { "type": "number" }          
            }
          }        
        example: |
          {
            "id":"11"
          }
    responses:
      200:
        body:
          application/json:
            schema: |
              schema: |
              - CategoryDishesResponse: {"$schema": "http://json-schema.org/schema",
                "type": "array",
                "items": { "$ref": "CategoryDishesResponseItem" }
              }
              - CategoryDishesResponseItem: |
                {
                  "type": "object",
                  "properties": {
                    "nid":  { "type": "number" },
                    "node_created":  { "type": "number" },
                    "img_path":  { "type": "string" },
                    "node_title":  { "type": "string" }
                  }
                }
            example: |
              [
                {
                  "nid": "29",
                  "node_created": "1432460612",
                  "img_path": "http://barashki.net/sites/default/files/styles/________170x145_/public/kare-yagnenka-v-souse-med-balzamik-1390_2.jpg",
                  "node_title": "Каре свинины с соусом BBQ"
                },
                {
                  "nid": "28",
                  "node_created": "1432460570",
                  "img_path": "http://barashki.net/sites/default/files/styles/________170x145_/public/kare-yagnenka-v-souse-med-balzamik-1390_0.jpg",
                  "node_title": "Каре баранины с коньячным соусом 35"
                },
                {
                  "nid": "27",
                  "node_created": "1432460525",
                  "img_path": "http://barashki.net/sites/default/files/styles/________170x145_/public/kare-yagnenka-v-souse-med-balzamik-1390.jpg",
                  "node_title": "Каре баранины с коньячным соусом 34"
                }
              ]

это только половина файла, я знаю что REST api это стандарт по которому пишут api, но я никогда с таким не сталкивался и я не могу понять как здесь можно хоть какуе то информацию вывести, может кто то подскажет?)

deivan 16.08.2015 10:30

Кратко: делаем HTTP запрос на некоторый урл поставщика АПИ, если брать пример то что-то вроде:
http://barashki.net/dishes.php?id=11
Он возвращает ответ в JSON формате, который джаваскрипт автоматически преобразует в объект с данными, который можно обрабатывать далее.
Примеров по этой теме - навалом.


Часовой пояс GMT +3, время: 21:41.