React.js URL GET 请求中的随机变量
React.js random variable in URL GET request
我正在使用 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
谁能告诉我为什么?
// 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);
});
});
我还不能发表评论所以我就在这里回答。
您已设置 cache: false
,因此 jQuery 附加时间戳,以便始终调用 JSON 的新副本。
但这不太可能是 404 错误的原因。
我正在使用 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
谁能告诉我为什么?
// 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);
});
});
我还不能发表评论所以我就在这里回答。
您已设置 cache: false
,因此 jQuery 附加时间戳,以便始终调用 JSON 的新副本。
但这不太可能是 404 错误的原因。