html - Why do I get XMLHTTPRequest error on the following JavaScript code? -


i trying native javascript , requesting html page using following js code, why return (in chrome) following?

xmlhttprequest cannot load /live-examples/temp.html. no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access.

the code using follows:

            var sendrequest = (function(){              // private member variables             var readystate = {                 unsent          : 0,                 opened          : 1,                 headers_recived : 2,                 loading         : 3,                 done            : 4             }              var status = {                 success : 200,                 not_modified : 304             }              // private member methods             function getdata(callback){                  var xhr = getxhr();                 if(!xhr){                     console.log('unable create xhr object.')                     return false;                 }                  // test readystate                 if(xhr.readystate !== readystate.done) return;                  if(xhr.status === status.success && xhr.status !== status.not_modified){                     callback(xhr.responsetext);                 }             }               function getxhr(){                  var xmlhttplist = [                     function(){ return new xmlhttprequest(); },                     function(){ return activexobject("msxml2.xmlhttp"); },                     function(){ return activexobject("microsoft.xmlhttp"); }                 ], xhr;                  for(var = 0; < xmlhttplist.length; i++){                     try{                         xhr = xmlhttplist[i];                         break;                     }catch(e){                         continue;                     }                 }                 return xhr();             }               return function(url, callback, postdata){                 var xhr = getxhr();                 if(!xhr) return;                  var method = (postdata) ? 'post' : 'get';                  xhr.open(method, url, true);                 xhr.setrequestheader('content-type','application/x-www-form-urlencoded');                 xhr.onreadystatechange = getdata(callback);                  xhr.send(postdata);             };          })();          function dealwithresponse(response){             // response xhr request             console.log('success response: ' + response);         }          // cross origin request error.         $(function(){             document.getelementbyid('myid').onclick = function(){ sendrequest('/live-examples/temp.html', dealwithresponse, 'get'); }         }); 

html code within body follows:

<button id="myid">i have button!</button> 

i checked the file 'temp.html' exist.

i in code trying show simple ajax call using native javascript in talk due present.

many in advance, quinton :)

you need use http when want access data using xmlhttprequest object.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -