简体   繁体   中英

Getting A Cross-Origin XmlHttpRequest to Return an XML File in HTML5

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\\n' + URL; response = warn; alert(warn); } } }; alert("before"); request.send(null); alert("after"); } return response; } 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM