Help to query a single information from a database user with JSON

1

This is the code that works with the img.php file

function loadContato(){
    var location = $('.result_File');
    var item = "";
    $.getJSON("img.php", function(dado){
        item += '<p>' + dado[0].username + '</p>';
        item += '<img src="avatar/'+dado[0].avatar+'">';
        location.html(item);
        console.log(item);
    });
}

json file information file img_trocar.php

[ { "avatar": "angela.jpg", "username": "angela vazquez", "email_user": "[email protected]" } ]

I'm having trouble with a request from a single line of user information

Here's how the photo shows before sending it to the database okay no problem

function imagemPreview(input) {
    var imgpreview = $('#mgpreview');
    var iconAVATAR = $('.avatar');
    if (input.files && input.files[0]) {
        var filerd = new FileReader();
        filerd.onload = function (e) {
            imgpreview.attr('src', e.target.result);
        };
        filerd.readAsDataURL(input.files[0]);
    }
    uploadFoto();
}

The problem is here in this function I'm trying to obiter the user information in JSON format with the $ .getJSON method plus as it's a single line I do not know how to do for () show the information on the page, HELP PLEASE, for security will not be able to show the user data here

function uploadFoto(){
    var items = "";
    $.getJSON('img_trocar.php', 'GET')
    .fail(function(){
        $('.result_File').html("ERRO!!");
    })
    .always(function(data){
        for(var i = 0; i < data; i++)
            {   
            items += '<img src="avatar/'+data.avatar+'" alt="avatar">';
            items += '<p>'+data[i].username+'</p>';   
            }
        $('.result_File').html(items);
    });
}
    
asked by anonymous 03.03.2018 / 19:15

1 answer

0

Since it is only a JSON object array of only 1 index, you can not need for , just call the value by the key picking the first and only array index ( data[0] ):

Example:

var data = [ { "avatar": "angela.jpg", "username": "angela vazquez", "email_user": "[email protected]" } ],
items = '<img src="avatar/'+data[0].avatar+'" alt="avatar">'
+ '<p>'+data[0].username+'</p>';   
console.log(items);
    
03.03.2018 / 20:29