javascript - jQuery ajax 自动完成不映射

标签 javascript jquery json ajax

我需要将此 json 数组映射到文本框并使用自动完成填充,下面是我认为有效的代码,fiddler 返回良好的结果,并且在控制台浏览器上没有错误

编辑:我在控制台中遇到错误:

Uncaught TypeError: Cannot read property 'length' of undefined

jQuery

$(document).ready(function () {
    $(function () {
        $("#destinationhotelcity").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "http://engine.hotellook.com/api/v2/lookup.json",
                    dataType: "json",
                    data: {
                        query: request.term, // input field value
                        lang: "en",
                        lookFor: "both",
                        limit: 10,
                    },
                    success: function (data) {
                        response($.map(data.hotels, function (item) {
                            return {
                                label: item.label + ', ' + item.locationName,
                                value: item.label + ', ' + item.locationName,
                                code: item.id
                            }
                        }));
                    }
                });
            },
            select: function (event, ui) {
                $("#destinationhotelcity").val(ui.item.value);
            },
            change: function (event, ui) {
                if (!ui.item) {
                    $("#destinationhotelcity").val("");
                }
            },
            onclick: function (event, ui) {
                $("#destinationhotelcity").val("");
            }
        });
    });
});

示例 JSON 数据

{
  "status": "ok",
  "results": {
    "hotels": [
      {
        "location": {
          "lon": 120.094396,
          "lat": 29.325235
        },
        "label": "Byland Star Hotel",
        "_score": 144247,
        "fullName": "Byland Star Hotel, Yiwu, China",
        "locationId": 25142,
        "id": "153465",
        "locationName": "Yiwu, China"
      },
      {
        "location": {
          "lon": 2.822413,
          "lat": 41.982377
        },
        "label": "Peninsular",
        "_score": 140926,
        "fullName": "Peninsular, Girona, Spain",
        "locationId": 3505,
        "id": "287519",
        "locationName": "Girona, Spain"
      },
      {
        "location": {
          "lon": 121.025466,
          "lat": 14.554966
        },
        "label": "The Peninsula Manila",
        "_score": 131960,
        "fullName": "The Peninsula Manila, Makati City, Philippines",
        "locationId": 24539,
        "id": "389082",
        "locationName": "Makati City, Philippines"
      },
      {
        "location": {
          "lon": 116.416382,
          "lat": 39.915437
        },
        "label": "The Peninsula Beijing",
        "_score": 129755,
        "fullName": "The Peninsula Beijing, Beijing, China",
        "locationId": 6679,
        "id": "10782",
        "locationName": "Beijing, China"
      },
      {
        "location": {
          "lon": 100.510923,
          "lat": 13.723085
        },
        "label": "The Peninsula Bangkok",
        "_score": 128178,
        "fullName": "The Peninsula Bangkok, Bangkok, Thailand",
        "locationId": 25949,
        "id": "20028",
        "locationName": "Bangkok, Thailand"
      },
      {
        "location": {
          "lon": 121.489021,
          "lat": 31.240765
        },
        "label": "The Peninsula Shanghai",
        "_score": 120824,
        "fullName": "The Peninsula Shanghai, Shanghai, China",
        "locationId": 6680,
        "id": "19937",
        "locationName": "Shanghai, China"
      },
      {
        "location": {
          "lon": 139.760374,
          "lat": 35.674786
        },
        "label": "The Peninsula Tokyo",
        "_score": 105459,
        "fullName": "The Peninsula Tokyo, Tokyo, Japan",
        "locationId": 25666,
        "id": "19960",
        "locationName": "Tokyo, Japan"
      },
      {
        "location": {
          "lon": 25.020123,
          "lat": 35.414669
        },
        "label": "Peninsula Resort & Spa",
        "_score": 103874,
        "fullName": "Peninsula Resort & Spa, Agia Pelagia, Greece",
        "locationId": 23282,
        "id": "315527",
        "locationName": "Agia Pelagia, Greece"
      },
      {
        "location": {
          "lon": -73.975325,
          "lat": 40.76168
        },
        "label": "The Peninsula New York",
        "_score": 97931,
        "fullName": "The Peninsula New York, New York City, New York, United States",
        "locationId": 20857,
        "id": "19944",
        "locationName": "New York City, New York, United States"
      },
      {
        "location": {
          "lon": 2.172499,
          "lat": 41.379624
        },
        "label": "Hotel Peninsular",
        "_score": 97678,
        "fullName": "Hotel Peninsular, Barcelona, Spain",
        "locationId": 3196,
        "id": "292590",
        "locationName": "Barcelona, Spain"
      }
    ],
    "locations": [

    ]
  }
}

最佳答案

response($.map(data.hotels, function (item) {

应该是:

response($.map(data.results.hotels, function (item) {

关于javascript - jQuery ajax 自动完成不映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34259438/

相关文章:

asp.net - 拖动控制 Flash 电影问题?

android - 如何从一个 JSONObject 获取值到另一个 JSONObject

java - 从 http ://www. autorenlexikon.lu 接收 JSON 列表

javascript - 基于分层(父子)数据的数据驱动面包屑

javascript - 使用 D3.js 随机创建的移动圆圈

javascript - 如何使工具提示在超宽元素上围绕光标 float

php - REST API Base64 图像 imagecreatefromstring() : Data is not in a recognized format

javascript - 为什么 或 &#8203 被随机插入到我的 html 正文中?

javascript - 在具有各种监听器的 for 循环中使用 addEventListener()

javascript - 将 dropzone.js 与 fancybox.js 相结合以提供上传照片的全屏 View