废话少数,直接上代码!!!!!!!!!!
html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.js"></script> </head> <body> <ul id="list"></ul> <script> var url = "http://japi.juhe.cn/tv/getCategory?key=1e90c38759b2271714f60133b04f75c4" var str = ""; $.ajax({ type: "get", contentType: "application/json; charset=utf-8;", dataType: "json", url: "ajax.php", success:function(data){ var len = data.result.length; // console.log(data.result.name) for(var i =0; i<len;i++){ var li = "<li data-id="+ data.result[i].id + ">"+ data.result[i].name+"</li>"; // console.log(`${data.result.name}123`) // var li= `<li data-id=${data.result[i].id}>${data.result[i].name}</li>` str +=li; } $("#list").html(str); } }) </script> </body> </html>
2.php文件:
<?php // cors 跨域 // 设置请求头部信息啊 header("Access-Control-Allow-Origin:*"); // 设置跨域方式 header("Access-Control-Allow-Method:GET"); //读取数据的地址 $URL = "http://japi.juhe.cn/tv/getCategory?key=1e90c38759b2271714f60133b04f75c4"; // 读取数据 $html = file_get_contents($URL); echo $html; ?>