Separate json response for tags

1

Personal I have the following ajax call:

$(".sec-tags").html(res.data.tags);

It is returning the call from the server, however the call is coming as follows:

tags: "coelho,teste,gato"

Everything is coming within a single string separated by commas. How do I separate these words and put each one inside an html tag:

<span class="tags">minha tag aqui</span>

My HTML is:

<div class="col-md-6">
    <div class="sec-tags">
        <span class="tags"></span>
    </div>
</div>
    
asked by anonymous 15.07.2016 / 22:32

2 answers

2

To separate this string into pieces you can do this:

var tags = res.data.tags.split(',');

But your comment makes me believe that it is an array, but it appears as a string when you convert to HTML with $(".tags").html(res.data.tags); .

I will assume that it is an array, ie

var tags = res.data.tags; 

So to mount this HTML you can do this:

var tags = res.data.tags; // ou no caso de ser string, usa como coloquei em cima: var tags = res.data.tags.split(',');
var spans = tags.map(function(tag){
    return '<span class="tags">' + tag + '</span>';
}).join('');
$(".sec-tags").html(spans);
    
15.07.2016 / 23:02
0

Your call is not coming with JSON, but standard text.

use the following line:

$aux = variavel_que_recebeu_o_texto.split(",");

It will have an array and can handle your result.

    
15.07.2016 / 22:34