最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

Working on the ReactJS tutorial I spinned up a local server with

>npm install -g http-server

>http-server -c-1

And got local server working fine located at http://localhost:8080

Though, when I attempted to use an AJAX call within one of my ponents, I got the following error in my chrome console:

  XMLHttpRequest cannot load http://localhost:8080/ment.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405.

This is the 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 is ing from here:

React.render(<CommentBox url="http://localhost:8080/ment.json" pollInterval={2000} />, document.getElementById('content'));

Working on the ReactJS tutorial I spinned up a local server with

>npm install -g http-server

>http-server -c-1

And got local server working fine located at http://localhost:8080

Though, when I attempted to use an AJAX call within one of my ponents, I got the following error in my chrome console:

  XMLHttpRequest cannot load http://localhost:8080/ment.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405.

This is the 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 is ing from here:

React.render(<CommentBox url="http://localhost:8080/ment.json" pollInterval={2000} />, document.getElementById('content'));
Share asked Jun 14, 2015 at 0:08 ApathyBearApathyBear 9,62515 gold badges59 silver badges90 bronze badges 1
  • Can you post the server endpoint code? The issue is the header for Access-Control-Allow-Origin needs to be on the server response. – Cymen Commented Jun 14, 2015 at 0:16
Add a ment  | 

1 Answer 1

Reset to default 3

The header 'Access-Control-Allow-Origin': '*' needs to be on the server response to the AJAX request (not on the client request). Here is an example of doing that 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, you can run it with --cors option.

发布评论

评论列表(0)

  1. 暂无评论