简体   繁体   English

从JSON获取数据不能与jquery load一起使用

[英]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.

相关问题 从 Json 数据中获取数据 - Fetching Data from Json Data 从JSON数组中获取数据 - Fetching data from JSON array 使用.load()获取数据时jQuery的问题 - Issue with jQuery when fetching data using .load() 单击从 JSON 文件中获取数据的 div 时,如何在 ReactPlayer 中加载视频? - How to load a video in ReactPlayer when clicking a div that is fetching data from JSON file? 错误**未捕获的类型错误:从 json 获取数据时无法读取未定义的属性“名词”** - Error **Uncaught TypeError: Cannot read property 'noun' of undefined** while fetching data from json 从 JSON 获取数据时无法读取未定义的属性(读取“params”)Uncaught TypeError - Cannot read properties of undefined (reading 'params') Uncaught TypeError whilte fetching data from JSON 将 Lambda 与 NodeJS 一起使用并从 Dynamo 数据库正确获取值,我无法处理收到的 json 数据 - Using Lambda with NodeJS and fetching value correctly from Dynamo db, I cannot process the json data recieved 无法使用JSON加载数据表 - Cannot load data table with JSON Ember应用程序无法从本地文件加载json数据 - Ember app cannot load json data from local file D3:无法使用 PHP 从数据库加载 json 数据 - D3: cannot load json data from database using PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM