javascript - Getting error message on while AJAX-get on localhost with node.js -
i'm trying use ajax piece of string local server made using node.js
, connection being done, status 200, working still can't piece of string. may provide me explanation of why happening?
p.s.: i'm still newbie node.js
node.js
server code:
// load http module create http server. var http = require('http'); // configure our http server respond hello world requests. var server = http.createserver(function (request, response) { response.writehead(200, {"content-type": "text/javascript"}); response.end("var = 'hello world';"); }); // listen on port 8000, ip defaults 127.0.0.1 server.listen(8000); // put friendly message on terminal console.log("server running @ http://127.0.0.1:8000/");
ajax
call code:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script> $.ajax({ async: 'false', type: 'get', url: 'http://127.0.0.1:8000/', success: function(data, textstatus){ console.log('done: request has been sent'); }, error: function(xhr, textstatus, errorthrown){ console.log('err: request failed \nstatus: '+textstatus+'\nerror: '+errorthrown); console.log(xhr); } }); </script>
console:
xmlhttprequest cannot load http://127.0.0.1:8000/. no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access. xhr failed loading: "http://127.0.0.1:8000/".send @ jquery-latest.min.js:4m.extend.ajax @ jquery-latest.min.js:4(anonymous function) @ err: request failed status: error error: object {readystate: 0, responsetext: "", status: 0, statustext: "error"}
cross domain
fails ajax call because of violation
of same origin policy
. therefore, need modify code , remove crossdomain : true,
. also, add timeout:5000,
or write async: false,
safe side.
it work then.
Comments
Post a Comment