javascript - CORS request not going through. Unsure why -
console telling me...
cross-origin request blocked: same origin policy disallows reading remote resource @ "example.com". can fixed moving resource same domain or enabling cors.
i've got xhr object created in createcorsrequest function , makecorsrequest function. request executes using url parameter php file contains code that's supposed allow access origin. below bits cors functions , php file.
// create xhr object function createcorsrequest(method, url) { var xhr = new xmlhttprequest(); if ("withcredentials" in xhr) { // xhr chrome/firefox/opera/safari xhr.open(method, url, true); } else if (typeof xdomainrequest != "undefined") { // xdomainrequest internet explorer xhr = new xdomainrequest(); xhr.open(method, url); } else { // cors not supported. xhr = null; } return xhr; } // helper method parse title tag response. function gettitle(text) { return text.match('<title>(.*)?</title>')[1]; } function makecorsrequest() { var url = "example.com/directory/file.php"; var xhr = createcorsrequest("post", url); if (!xhr) { console.log("cors not supported"); return; } //response handlers. xhr.onload = function() { var text = xhr.responsetext; var title = gettitle(text); console.log("response cors request " + url); }; xhr.onerror = function() { console.log("whoops, there error making request."); };
php file
if (isset($_server['http_origin'])) { header("access-control-allow-origin: {$_server['http_origin']}"); //{$_server['http_origin']} can changed specific sites allow access header('access-control-allow-credentials: true'); header('access-control-max-age: 86400'); // cache 1 day } // access-control headers received during options requests if ($_server['request_method'] == 'options') { if (isset($_server['http_access_control_request_method'])) header("access-control-allow-methods: get, post, options"); if (isset($_server['http_access_control_request_headers'])) header("access-control-allow-headers: {$_server['http_access_control_request_headers']}"); exit(0); }
the overall gyst here page 1 loads , data page 1 written xml file, upon clicking hyperlink page 2 on different server, user can submit form parses xml file specific node. javascript file containing cors request , php file accessed upon loading of page 1. code seems run except 2 places...
on page 2, javascript parses xml file has jquery in , error says:
typeerror: $(...).validate not function
when far know, that's legit function. error in console comes before cors blocking notification not sure if plays part.
Comments
Post a Comment