如何使用php和ajax加载数据库mysql json数组

时间:2017-09-04 21:46:05

标签: javascript php jquery mysql ajax

我想用AJAX将这个JSON数据加载到我的网站中。我该怎么办?

我的HTML代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <h1>Authors</h1>
  <ul id="authors"></ul>
</body>
</html>

我的Ajax代码:

function createNode(element) {
      return document.createElement(element);
  }

  function append(parent, el) {
    return parent.appendChild(el);
  }

  const xx = document.getElementById('authors');
  const url = 'https://randomuser.me/api/?results=2';
  fetch(url)
  .then((resp) => resp.json())
  .then(function(data) {
    let authors = data.results;
    return authors.map(function(authorz) {
      let span = createNode('span');

      span.innerHTML = '${authorz.gender}<br>';

      append(xx, span);
    })
  })
  .catch(function(error) {
    console.log(JSON.stringify(error));
  });

这可以按预期工作,但我想从

更改我的链接
https://randomuser.me/api/?results=2

http://jiyo-hotel.com/ttm/mutiara.php

span.innerHTML = '${authorz.gender}'

span.innerHTML = '${authorz.kata}'

但它不起作用。

0 个答案:

没有答案