javascript - ReactJS localhost Ajax call: No 'Access-Control-Allow-Origin' header -


working on reactjs tutorial spinned local server

>npm install -g http-server  >http-server -c-1 

and got local server working fine located @ http://localhost:8080

though, when attempted use ajax call within 1 of components, got following error in chrome console:

  xmlhttprequest cannot load http://localhost:8080/comment.json. no 'access-control-allow-origin' header present on requested resource. origin 'null' therefore not allowed access. response had http status code 405. 

this ajax call snippet:

var commentbox = react.createclass({                     loadcommentsfromserver: function(){                         $.ajax({                             url: this.props.url,                             datatype: 'json',                             cashe: false,                             crossdomain:true,                             headers: {'access-control-allow-origin': '*'},                             success: function(data){                                 this.setstate({data: data});                             }.bind(this),                             error: function(xhr, status, err){                                 console.error(this.props.url, status, err.tostring());                             }.bind(this)                         });                     }, 

this.props.url coming here:

react.render(<commentbox url="http://localhost:8080/comment.json" pollinterval={2000} />, document.getelementbyid('content')); 

the header 'access-control-allow-origin': '*' needs on server response ajax request (not on client request). here example of doing on vanilla nodejs using http:

var http = require('http');  var server = http.createserver(function(request, response) {   response.writehead(200, {     'access-control-allow-origin': '*',     'content-type': 'application/json'   });   response.end('hi'); }).listen(8080); 

for http-server, can run --cors option.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -