xml声明之前不能有空格空行

  注意:XML 声明通常在 XML 文档的第一行出现。 XML 声明不是必选项,但是如果使用 XML 声明,必须在文档的第一行,前面不得有空格空行。

        出现问题场景:用ajax请求服务端的xml数据

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>ajax请求xml数据</title>
</head>
<body>
	<script>
		var xhr=new XMLHttpRequest();
		xhr.open('GET','data.php');
		xhr.send();
		xhr.onreadystatechange=function(){
			if(this.readyState!=4) return;
			// console.log(this.responseText);
			//this.responseXML专门获取服务端返回的xml数据,操作方式是通过dom的方式操作
			//但是需要服务端的响应头中的Content-Type设置为application/xml
			console.log(this);
			
		}
	</script>
</body>
</html>

注意:下面的xml声明前有空行

<?php 
//不管服务端返回的数据是哪种类型,都在content-type写清楚
header('Content-Type: application/xml');
 ?>
 
<?xml version="1.1" encoding="utf-8"?>
<person>
	<student>石羊</student>
	<age>18</age>
	<gender>男</gender>
</person>

这时候输出的内容responseXML的值为null,出现了不是想要的结果!!!

去掉xml声明前的空格后:出现了想要的结果。

猜你喜欢

转载自blog.csdn.net/qq_27346075/article/details/83246758
今日推荐