小编Wal*_*mad的帖子

Socket.io + Node.js跨源请求被阻止

我正在使用node和socket.io来编写聊天应用程序.它在Chrome上工作正常但mozilla在启用跨源请求时出错.

跨源请求已阻止:同源策略不允许在http://waleedahmad.kd.io:3000/socket.io/?EIO=2&transport=polling&t=1401964309289-2&sid=1OyDavRDf4WErI-VAAAI上读取远程资源.这可以通过将资源移动到同一域或启用CORS来解决.

这是启动节点服务器的代码.

var express = require('express'),
    app = express(), 
    server = require('http').createServer(app),
    io = require('socket.io').listen(server),
    path = require('path');
server.listen(3000);

app.get('/', function(req, res) {
    res.sendfile(__dirname + '/public/index.html');
});
Run Code Online (Sandbox Code Playgroud)

在客户端.

var socket = io.connect('//waleedahmad.kd.io:3000/');
Run Code Online (Sandbox Code Playgroud)

HTML页面上的脚本标记.

<script type="text/javascript" src="//waleedahmad.kd.io:3000/socket.io/socket.io.js"></script>
Run Code Online (Sandbox Code Playgroud)

我也在app根目录中使用.htaccess文件.(waleedahmad.kd.io/node).

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Run Code Online (Sandbox Code Playgroud)

sockets node.js cors socket.io

52
推荐指数
10
解决办法
7万
查看次数

在$ .ajax()请求之后,反应setState()不更新状态

我正在使用react-react进行反应.在使用IndexRoute上的onEnter Asynchronous挂钩检查身份验证后,将呈现App组件.应用程序组件具有初始状态auth,在呈现时设置为undefined.auth状态被传递给Navbar组件作为prop,它将用于决定是否显示登录,注册和注销链接.

当App组件完成渲染时,componentDidMount()再次进行ajax调用以检查用户是否经过身份验证.在回应时它会改变状态.在从ajax请求状态更改后,我正在将状态记录到控制台,this.setState()方法不会更改状态,但仍会以某种方式触发Navbar组件上的componentWillReceiveProps()方法,并且this.props.auth值仍未定义.

// Checks Authentication Asynchronously 
isAuthenticated(nextState, replace, callback) {
    $.ajax({
        type : 'GET',
        url : '/auth',
        success : function(res){
            if(!res){
                callback(replace({ pathname: '/login', query: { auth: 'false' } }));
            }else{
                callback();
            }
        }
    });
};

// routes
var routes = (
    <Router history={browserHistory}>
        <Route path="/" component={require('./components/app')}>
            <IndexRoute component={require('./components/dashboard/index')} onEnter={Auth.isAuthenticated}/>

            <Route path="/register"
                   component={require('./components/authentication/register')}
                   onEnter={Auth.isNotAuthenticated} />

            <Route path="/login"
                   component={require('./components/authentication/login')}
                   onEnter={Auth.isNotAuthenticated}/>

            <Route path="*"
                   component={require('./components/404/404')}/>
        </Route>
    </Router>
);

// App
const App = React.createClass({

    getInitialState(){
        return …
Run Code Online (Sandbox Code Playgroud)

reactjs react-router

6
推荐指数
2
解决办法
1万
查看次数

Java:多个线程通过同步方法同时访问变量来减少静态变量

我正在尝试同步我的Person类方法,以便我的静态计数器变量一次减少一个线程.

public class Person extends Thread {

    private static int count = 10;

    public void decrement() {
        synchronized(Person.class) {
            count--;
        }

    }

    public int getCount() {
        return count;
    }

    public void run(){
        while( count > 0){
            this.decrement();
            System.out.print(this.getCount() + ",");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要课程.每个线程将通过synchronized方法递减到静态计数器,以避免多个线程访问相同的资源.

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Person p1 = new Person();
        Person p2 = new Person();
        Person p3 = new Person(); …
Run Code Online (Sandbox Code Playgroud)

java static multithreading synchronization synchronized

6
推荐指数
2
解决办法
841
查看次数

PHP:生成Laravel Paginator Secure(HTTPS)链接

我正在使用Laravel 4.2通过HTTPS开发一个应用程序,具有安全路由和重定向.我正在使用Paginator对结果进行分页,但是在视图中呈现的链接指向http页面,我们如何强制Paginator生成https链接?

https paginator laravel

5
推荐指数
2
解决办法
3774
查看次数

ReactJs:是否可以按类名称将DOM呈现给文档?

我正在学习ReactJs并且非常喜欢它.我想问一下我们是否可以按类名将虚拟DOM渲染到文档中.

React.render(
    <CommentBox url="data/comments.json" pollInterval={2000} />,
    document.getElementById('class')
);
Run Code Online (Sandbox Code Playgroud)

我试图通过

getElementByClassName( '类')

作为反应的渲染方法的第二个参数,它不起作用.React是否仅将DOM呈现给仅具有ID的节点,或者是否有使用类的节点的解决方法?

javascript dom reactjs

4
推荐指数
1
解决办法
1万
查看次数