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

我有两个不同的项目,不知何故我必须将数据从 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>

我是新手,如有错误请多多包涵,谢谢!


慕姐8265434
浏览 36回答 1
1回答

炎炎设计

您可以使用JSON.stringify打印对象的内容。$('#results').html(JSON.stringify(json.data, null, 4));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5