简体   繁体   English

URL GET请求中的React.js随机变量

[英]React.js random variable in URL GET request

I'm using React.js to fetch a list of comments from my Express server but my GET URL keeps getting appended with random variable strings like "?_1441474975073" 我正在使用React.js从Express服务器中获取评论列表,但我的GET URL不断添加诸如“?_1441474975073”之类的随机变量字符串

// Error in Chrome console

GET http://localhost:4000/comments.json?_=1441474975073 404 (Not Found)
/comments.json error Not Found

Can someone tell me why?? 有人可以告诉我为什么吗?

// main.js
React.render(
    <CommentBox url="/comments" />,
    document.getElementById('reactComment')
);

var CommentBox = React.createClass({
    componentDidMount: function() {
        $.ajax({
            url: this.props.url,
            dataType: 'json',
            type: 'GET',
            cache: false,
            success: function(data) {
                this.setState({comments: data})
            }.bind(this),
            error: function(xhr, status, err) {
                console.error(this.props.url, status, err.toString());
            }.bind(this)
        });
    },
    ...
});

// server.js
app.get('/comments', function(req, res) {
    Comments.find().exec(function(err, data) {
        if (err) throw err;
        res.json(JSON.parse(data);
    });
});

I'm not able to leave comments yet so I'll just answer here. 我还不能发表评论,所以我只在这里回答。

You have cache: false set so jQuery appends a timestamp so that a fresh copy of the JSON is always called. 您已经设置了cache: false设置为cache: false因此jQuery会附加一个时间戳,以便始终调用JSON的新副本。

This isn't likely the cause of the 404 error, however. 但是,这不太可能导致404错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM