如何从内容json信息的url中使用和自动填充字段

时间:2018-02-04 13:11:38

标签: php json autocomplete

我正在尝试从url中自动填充一个包含json信息的字段,我发现很多例子,我不知道我的错误在哪里。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
   $("#tags").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "http://camino-rest-service.cloudhub.io/caminoAPI/GetCamino",
            data: { query: request.term },
            success: function (data) {
                var transformed = $.map(data, function (el) {
                    return {
                        label: el.ubicaciones
                    };
                });
                response(transformed);
            },
            error: function () {
                response([]);
            }
        });
    });
});
  } );
  </script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


</body>
</html>

JSON示例

  [
    {
      "ubicaciones": "San Gabriel, Provincia de Carchi"
    },
    {
      "ubicaciones": "El Ángel, Provincia de Carchi"
    },
    {
      "ubicaciones": "Ambuquí, Provincia de Imbabura"
    }
  ]
有些人可能会说我的错误是谁?我尝试了解我知道的事情

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$( function() {
   $("#tags").autocomplete({
    source: function (request, response) {
        $.ajax({
        type: "POST",
            url: "http://camino-rest-service.cloudhub.io/caminoAPI/GetCamino",
              
        dataType: "json",
           
            success: function (data) {
                var transformed = $.map(data, function (el) {
                    return {
                        label: el
                    };
                });
                response(transformed);
            },
            error: function () {
                response([]);
            }
        });
    }
});
  });
&#13;
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>

  </script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


</body>
</html>
&#13;
&#13;
&#13;

您在代码中遇到语法错误,您提供的链接也不响应 POST GET 方法(我只修复语法问题) < / p>