[英]fetching data from JSON cannot work with jquery load
I intend to fecthing data from JSON with ajax to html with append, and its work really good. 我打算使用附加功能将带有ajax的数据转换为带有追加的html,并且其工作非常好。 But I'm having a problem when I trying to load html from external file, the variable cannot read with jquery load.
但是当我尝试从外部文件加载html时遇到问题,变量无法用jquery加载读取。
How I can read the variable from external file with jquery load to show in HTML? 如何从jquery load中读取外部文件中的变量以在HTML中显示?
$.ajax({ url:'person.json', dataType:'json', type:'get', cache: false, success:function(data){ $(data.person).each(function(index, value){ //$("#loadJson").append('<p><strong>Name : </strong>'+value.first_name+'</br><strong>Company: </strong>'+value.company+'</br> <strong>Email: </strong>'+value.email+' </p>') $("#loadJson").load('data.html'); }) } })
<p><strong>Name: </strong>'+value.first_name+'</br><strong>Company: </strong>'+value.company+'</br> <strong>Email: </strong>'+value.email+' </p>
You can load html as a template for the js variable, stick with appending build html 您可以加载html作为js变量的模板,坚持使用附加的构建html
success:function(data){
$(data.person).each(function(index, value){
$("#loadJson").append('<p><strong>Name : </strong>'+value.first_name+'</br><strong>Company: </strong>'+value.company+'</br> <strong>Email: </strong>'+value.email+' </p>');
});
}
or use a templating framework/engine 或使用模板框架/引擎
1- have you checked the path? 1-你检查了路径吗? Make sure it is working.
确保它正常工作。
2- are you sure the response type is json? 2-你确定响应类型是json吗? If not, try this:
如果没有,试试这个:
data = JSON.parse(data);
// bind data here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.