Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用XMLHttpRequest访问XML文件_Javascript_Xml_Xmlhttprequest - Fatal编程技术网

Javascript 使用XMLHttpRequest访问XML文件

Javascript 使用XMLHttpRequest访问XML文件,javascript,xml,xmlhttprequest,Javascript,Xml,Xmlhttprequest,我目前在从我的XML文件中提取信息时遇到问题。这是相关代码 var name = null; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState === 4 && xhttp.status === 200) { myFunction(xhttp); } }; xhttp.open("GET", "NAME.xml", true)

我目前在从我的XML文件中提取信息时遇到问题。这是相关代码

var name = null;

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (xhttp.readyState === 4 && xhttp.status === 200) {
  myFunction(xhttp);
  }
};

xhttp.open("GET", "NAME.xml", true);
xhttp.send();

function myFunction(xml) {
  var xmlDoc = xml.responseXML;
  var x = xmlDoc.getElementsByTagName("uName")[0];
  var y = x.childNodes[0];
  name = y.nodeValue;
}

$.ajax({
  // Instructions to connect to requested XML file requested by user
  type: "GET",
  url: name,
  dataType: "xml",
  success: function(xml){

  // ...

我试图从另一个XML文件中提取一个XML文件的名称。如果在
url
字段中使用所需XML文件的实际名称,而不是使用名称,则代码工作正常。但是,在这个
url
空间中使用变量时遇到问题。我知道
XMLHttpRequest
是异步的,上面的代码使用null值提取name变量。订购代码的最佳方式是什么,这样我就可以成功地从
name.xml
中提取名称。知道XML文件和代码都位于NetBeans项目的同一站点根目录中可能会很有用。是否有明显的遗漏?

Xmlhttprequest是异步的。$。在调用myFunction之前将调用ajax。本机xmlhttprequest和jquery.ajax的有趣组合。只需将
$.ajax({
放在
myFunction()中