Bring images with ajax

Asked

Viewed 847 times

0

It is the following personal, I managed to store the links of the images in the database and also stored this images in a folder of my computer. so I was wondering how can I take these images with javascript and play them on my site. More or less so I would consult these images in the bank and get the link from her, with this link I wanted to be able to play on my page starting from java script, I am getting these links with the ajax, I’m just not knowing how to pass the image to the site, the description I got, how I do it is possible to do?

$(document).ready(function(){

 var imageName;
 
   var postagens = function(){
		

		$('#retorno').empty();		

		$.ajax({
			method : 'GET',
			url :'postagens.php',
			dataType : 'json',
			complete: function(dados){

					response = dados.responseJSON;

					for(var i = 0 ; response.length > i; i++){
					//document.getElementById("retorno").innerHTML = response[i].descricao;
					$('#retorno').append('<p>'+ response[i].descricao +'</p> <br>');
					

					}

			} 

		});
}

   

});
</div>

            <div>
                    <form id="conteudo" >

                         <div class="form-group">
                                <label for="exampleTextarea"></label>
                                <textarea class="form-control" id="exampleTextarea" rows="3"></textarea>
                        </div>
                            <button type="submit" class="btn btn-primary" id="btnPostar" name="btnPostar" >Postar</button> 
                            <input type="file" name="btnImage" id="btnImage"  class="btnImage" accept="image/*">
                            <hr size="1"> 

                            <p id="retorno"  name="retorno" > </p> <br>
                            <img  id="imagem"  src="">
                            
                    </form>
                
                
              </div>

  • Welcome, add inside the for , $("#imagem").attr("src", response[i].imagem);, but I have a question, because to be using the for ? It is necessary for what you are doing ? For if it is only returned a result, it is not necessary to use it.

  • Vlw ai guy. and the for type to work as posts!!!

1 answer

0


Following the same idea you did with the description.

Let’s assume your bank return is imagem, would do so...

$.ajax({
   type: 'post',
   url: 'SUA URL',
   success: function(data){
      $('#retorno').append('<img src="'+data.imagem+'">');
   }
}) 

In this way, a new image will always be created with the path that is in the bank.

  • Vlw ai amigo!!!

  • @Igormartins Do not forget to give +1 and mark as solved if you solve your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.