jQuery中Ajax调用后台接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24349695/article/details/76130999

直接上代码。获取数组中的数据imageUrl

<html>

<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var text;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

    text = xmlhttp.responseText;


  //  text = ‘{"news":[{"imageUrl":"https://www.baidu.com/","newsUrl":"https://www.baidu.com/","newsId":"0"},{"imageUrl":"https://www.baidu.com/","newsUrl":"https://www.baidu.com/","newsId":"0"}]}’;

    var jsonObj = JSON.parse( text );  // JSON.parse(); 方法
    document.getElementById("myDiv").innerHTML=jsonObj.news[1].imageUrl;
    }
  }
xmlhttp.open("GET","URL地址",true);
xmlhttp.send();
}
</script>
</head>
<body>


<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">通过 AJAX 改变内容</button>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_24349695/article/details/76130999