简体   繁体   中英

separate JSON response data and replace some html tag

I want to retrieve data from database and it's display in the div tag with separate line.

This is my data in database

  • column name - prod_desc
  • data - Camera:25MP | Memory:32GB | Battery: 3400mAh | Ram: 3GB
  • data type - varchar

I want to display like this

  • Camera:25MP
  • Memory:32GB
  • Battery: 3400mAh
  • Ram: 3GB

 var prodid = $("#prod_id").val(); var url = "lib/function.php?type=getProductDetails"; $.ajax({ method:"POST", url:url, data:{prodid:prodid}, dataType:"json", success:function (result) { $("#prod_desc").html(result.prod_desc); }, error:function (eobj, etxt, err) { console.log(etxt); } });

this is the division

 <div id='prod_desc'> display all data as a list here </div>

I assume from your question that your response is a string. We can use data as an example string variable.

Use let dataArray = data.split("|") . Then you will have an array with following values ["Camera:25MP", "Memory:32GB", "Battery: 3400mAh"...]

So dataArray[0] will be "Camera:25MP" , dataArray[1] will be "Memory: 32GB" etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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