Generate JSON from a URL and Search by Location

-1

Well, I would like to know if there is any possibility of showing a JSON list from a URL and in the same url they generate the following values: nmConveniado , strong , ListEndereco .

I tried to use the following script but did not succeed:

<script>

// Users data in json format
var userData = URL: "http://url.com/arquivo.json" };
// user's input for search
var searchVal = '';

$(function(){
      // if text box value is not null, then darken reset icon
      $(".slinput input").keyup(function(){
        var val = $(this).val();   
        if(val.length > 0) {
           $(this).parent().find(".right-icon").css('color','#555');
        } else {
          $(this).parent().find(".right-icon").css('color','#ccc');
        }
      });

      // if user click on reset icon, clear text field
      $(".slinput .right-icon").click(function(){
        $(this).parent().find("input").val('');
        $(this).css('color','#ccc');
        loadData(userData);
      });

      loadData(userData);
});

// Displaying Information to Users
function loadData(data) {
    var htmlData = '';
    $.each(data, function(index, val){
        htmlData += '<div class="media user">'+
        '  <div class="media-left">'+
        '    <a href="#">'+
        '      <img class="media-object" src="'+val.listaImagem+'" alt="...">'+
        '    </a>'+
        '  </div>'+
        '  <div class="media-body">'+
        '    <h4 class="media-heading">'+val.nmConveniado+'</h4>'+
        '    '+val.place+
        '  </div>'+
        '</div>';
    });
    $("#users").html(htmlData);
}

// Search users data based input search keyword
function searchUsers() {
    var val = $("#searchInput").val();
    if(val == searchVal) {
        return; 
    } else {
        searchVal = val;
        var searchResults = {};
        searchResults = [];
        $.each(userData, function(i, v) {
            if (v.nmConveniado.toLowerCase().indexOf(val) != -1) {
                searchResults.push(v);  
            }
        });
        loadData(searchResults);    
    }
}
</script>

In the same file I will try to filter the list by name, city, category.

    
asked by anonymous 20.06.2018 / 00:17

1 answer

2

You need to start by calling the URL via ajax, var userData = URL: "http://url.com/arquivo.json" }; will not work, with jQuery looks like this:

 var userData = '';

 $.ajax({
    url: "http://url.com/arquivo.json",
    success: function (json) {
        userData = JSON.parse(json);
    },
    error: function (xhr) {
        console.log(xhr);
    }
});

What we do here is call the URL and get the response in success in the successful case, or in error if some problem. The function in success will parse the return of the URL and save in the variable userData as an object.

    
20.06.2018 / 16:58