doujiao7325 2017-03-09 13:05
浏览 69

获取跨源XmlHttpRequest以返回HTML5中的XML文件

EDIT: REMEMBER TO ALWAYS CHECK YOUR NETWORK TAB FOR ERRORS, MY POSTED ANSWER ELABORATES THAT THIS WAS A STUPID QUESTION ON MY PART, THANKS epascarello

I'm writing some backend code for an HTML5 website and I can't get this very important function to work. The site will often need to dynamically pull PHP-generated XML files from a different server. I'm pretty sure that server has Access-Control-Allow-Origin set to allow requests from this server. I will verify with my boss but he's out for a couple days and honestly it's more likely something I'm doing wrong. Does anyone see something wrong with this code?

Notice I have an alert at the bottom before request.send(null) and after. .send() works fine and the function completes if I request an XML file from the same server, but if I request from the other I get the "before" alert but not the "after". I am also seeing no errors or other alerts displayed to me.

Notes:

There are some references to global variables and such in other .js files. I've determined that including them would be pointless and just clutter the question.

'cross' is just a boolean for whether the request is cross origin, to another server.

function createXmlHttpRequestObject(cross) {
  if(cross === undefined) {
    cross = false;
  }
  
  var xmlHttp;

  if(cross) {
    if(window.XDomainRequest) {
      try {
        xmlHttp = new window.XDomainRequest();
      } catch(e) {
        xmlHttp = false;
      }
    } else {
      xmlHttp = new XMLHttpRequest();
    }
  } else {
    if(window.ActiveXObject) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xmlHttp = false;
      }
    } else {
      try {
        xmlHttp = new XMLHttpRequest();
      } catch(e) {
        xmlHttp = false;
      }
    }
  }

  if(!xmlHttp) {
    alert ("Error: CREATING XMLHTTP REQUEST OBJECT FAILED");
  } else {
    return xmlHttp;
  }
}

//receives xml object from file
function getXmlObject(URL) {
  //if url contains any of site root, then it is not cross domain
  var cross = false;
  if(!URL.includes(url_home)) {
    cross = true;
  }
  
  var request = createXmlHttpRequestObject(cross);
  var response;
  
  if(window.ActiveXObject || window.XDomainRequest) {
    request.onload = function(){
      response = request.responseXML;
    };
    request.open('GET',URL,false);
    request.send(null);
  } else {
    request.open('GET',URL,false);
    request.onreadystatechange = function(){
      if(request.readyState == 4) {
        if(request.status == 200) {
          response = request.responseXML;
        } else {
          var warn = 'NOT FOUND
' + URL;
          response = warn;
          alert(warn);
        }
      }
    };
    alert("before");
    request.send(null);
    alert("after");
  }
  
  return response;
}

</div>
  • 写回答

1条回答 默认 最新

  • dqc3469 2017-03-09 13:33
    关注

    Found in the network tab confirmation that there is in fact no Access-Control-Allow-Origin flag on the server I'm trying to access. I'm gonna have to talk to the right people and see if that can change. Still, if this code can be improved or if there's still an issue here I'm not seeing please point it out.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度