node.js - XMLHttpRequest cannot load http://127.0.0.1:1337/. No 'Access-Control-Allow-Origin' header is present on the requested resource -
node.js - XMLHttpRequest cannot load http://127.0.0.1:1337/. No 'Access-Control-Allow-Origin' header is present on the requested resource -
i seek send , receive info html page angular.js , locally running node.js server
class="snippet-code-js lang-js prettyprint-override">app.controller("maincontroller", function maincontroller($scope, $http){ $scope.postit = function() { //alert("posting..."); $http.post('http://127.0.0.1:1337/', {msg:'hello angular.js!'}) .success(function onsuccess(response){ console.log(response); $scope.$apply(function(){ $scope.testdata = json.parse(response); console.log($scope.testdata); }); }).error(function onerror(){ console.log("ajax failed!"); }); }; });
class="snippet-code-html lang-html prettyprint-override"><div id='content' ng-app='mytutorialapp' ng-controller='maincontroller'> <p> <a ng-click="postit()"> click here load data. </a> </p> </div> <script src="bower_components/angular/angular.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <script src="maincontroller.js" type="text/javascript"></script>
then error:
xmlhttprequest cannot load http://127.0.0.1:1337/. no 'access-control-allow-origin' header nowadays on requested resource. origin 'null' hence not allowed access.
i seek apply headers app.js
var app = angular.module('mytutorialapp', [], function($httpprovider) { $httpprovider.defaults.headers.post['access-control-allow-origin'] = 'http://127.0.0.1:1337/'; });
but not take effect.
node.js code
var http = require('http'); http.createserver(function handler(req, res) { console.log(req.method, req.url, req.httpversion, req.headers); res.writehead(200, {'content-type': 'text/plain'}); res.end('hello node.js\n'); }).listen(1337, '127.0.0.1'); console.log('server running @ http://127.0.0.1:1337/');
if using angular send ajax request localhost node.js server, should set access-control-allow-origin on target server accepting ajax request i.e. localhost, don't post ajax request.
node.js angularjs http xmlhttprequest cors
Comments
Post a Comment