简体   繁体   English

GetJSON jquery 返回未定义

[英]GetJSON jquery returns undefined

I am trying to get my search box to work and do a getJSON on text search and title.我正在尝试让我的搜索框工作并在文本搜索和标题上执行 getJSON。 but in the console log, I get text=undefined?title=undefined.但在控制台日志中,我得到 text=undefined?title=undefined。 so it is not displaying any JSON.所以它不显示任何 JSON。 Not sure if my click is working correctly or if I have to make my JSON objects?不确定我的点击是否正常工作,或者我是否必须制作我的 JSON 对象?

Script脚本

 <script>
     var searchstring = $('input[type="text"]', this).val();
     var url = "https://data.edu/api/v1/metadata";

     url += "?text=" + searchstring;
     url += "?title=" + searchstring;


    $(document).ready(function() {
        $('button[type="button"]').click(function(event){

           $.ajax({
            type: "GET",
            url: url,

            success: function(res){
                console.log(res);
                  var items = res.data.metadata;
                  var ins = "";
                  for (var i = 0; i < items.length; i++){
                    ins += "<div>";
                    ins += "Title" + items[i].title;
                    ins += "Title" + items[i].title;
                    ins += "Title" + items[i].title;
                    ins += "</div><br />";
                  };
                  $('#results').html(ins);
                }

            });         
        });
     });

  </script>

html html

              <form class="destinations-form"  role="search" >
                 <div class="input-line">
                    <input id="searchForm"  type="text"  class="form-input check-value" placeholder="Search Documents" />

                    <button type="button"  class="form-submit btn btn-special"  "</button>

                 </div>
              </form>
               <div class="container">
                  <div class="hero-text align-center">
                     <div id="results"></div>
                  </div>
              </div>

json json

data: [
    {
        collection_id: "ADGM-1552427432270-483",
        metadata:{
                   year: "2019",
                   files: text ,
                   title: text,
                },

The problem is because you only read the values from the field when the page first loads and it is empty.问题是因为您只在页面首次加载时读取字段中的值并且它是空的。 To fix this, move that logic inside the click handler.要解决此问题,请在click处理程序中移动该逻辑。

The next issue is that you should remove this from $('input[type="text"]', this) .下一个问题是您应该从$('input[type="text"]', this)删除this You don't need a contextual selector here, and this one is incorrect regardless.您在这里不需要上下文选择器,无论如何,这个选择器是不正确的。

Also note that a valid querystring starts with ?另请注意,有效的查询字符串以? and separates each value with & , so your url concatenation needs to be amended slightly.并用&分隔每个值,因此您的url连接需要稍作修改。 In addition you shouldn't update the url value on every click.此外,您不应在每次点击时更新url值。 If you do it this way your AJAX request will only work once.如果您这样做,您的 AJAX 请求将只工作一次。

Lastly the metadata in your response is an object, not an array.最后,您的响应中的metadata是一个对象,而不是一个数组。 data is the array so you need to loop over that instead. data是数组,因此您需要对其进行循环。 The loop can also be simplified by using map() .也可以使用map()来简化循环。 Try this:尝试这个:

$(document).ready(function() {
  const url = "https://data.edu/api/v1/metadata";

  $('button[type="button"]').on('click', function(e) {
    let searchstring = $('input[type="text"]').val();
    let requestUrl = url + `?text=${searchstring}&title=${searchstring}`;

    $.ajax({
      type: 'GET',
      url: requestUrl,
      success: function(res) {
        let html = res.data.map(item => `<div>Title ${item.metadata.title}</div><br />`);
        $('#results').html(html);
      }
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM