为了账号安全,请及时绑定邮箱和手机立即绑定

从 JSON API(从一台服务器)获取数据并将其显示在 HTML 上(到另一台服务器)

从 JSON API(从一台服务器)获取数据并将其显示在 HTML 上(到另一台服务器)

慕姐8265434 2023-09-18 15:53:09
我有两个不同的项目,不知何故我必须将数据从 Project1 传递到 Project2。所以我决定将数据库中的Project1数据作为JSON API,并使用Project2的HTML页面来调用它。但是数据没有成功显示。JSON 文件如下所示:{    "data": [        {            "title": "Corporate Finance",            "responsibilities": "Responsible for financial accounting and reporting, internal controls, budgetary control, variance analysis.",            "requirements": "Candidate must possess at least a Degree in Accountancy or Professional Degree or equivalent.",            "status": "1"        },        {            "title": "Group Reporting Accountant",            "responsibilities": "We are seeking a Group accountant to take full financial responsibility for the Group Consolidation and Reporting. Reporting to the Regional Financial Controller, you will be responsible for formulati",            "requirements": "Prepare monthly financial statements for the business and to consolidate the group accounts.",            "status": "1"        }    ]}我尝试在Project2 HTML页面下使用AJAX来发出请求:$.ajax({        url: 'http://localhost/dbpass/api/post/read.php',        dataType: "text",        success: function(data) {            // Parse JSON file            var json = $.parseJSON(data);            //Store data into a variable            // Display             $('#results').html(json.data);        }      });并在 HTML 上显示它:<span id="results"></span>我是新手,如有错误请多多包涵,谢谢!
查看完整描述

1 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

您可以使用JSON.stringify打印对象的内容。

$('#results').html(JSON.stringify(json.data, null, 4));


查看完整回答
反对 回复 2023-09-18
  • 1 回答
  • 0 关注
  • 35 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信