From 32739d3a8a762aecbcd81a6d4c9c312ef6860d29 Mon Sep 17 00:00:00 2001 From: Joey Yang Date: Fri, 3 Jan 2014 20:45:58 -0800 Subject: [PATCH] switch jQuery for XMLHttpRequest; keep original jQuery for reference --- scripts/example.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/scripts/example.js b/scripts/example.js index 05f4bc5..3849aa8 100755 --- a/scripts/example.js +++ b/scripts/example.js @@ -16,25 +16,40 @@ var Comment = React.createClass({ var CommentBox = React.createClass({ loadCommentsFromServer: function() { - $.ajax({ - url: this.props.url, - success: function(data) { - this.setState({data: data}); - }.bind(this) - }); + var req = new XMLHttpRequest(); + req.onload = function(){ + if (req.readyState !== 4 || req.status !== 200) return; + this.setState({data: JSON.parse(req.responseText)}); + }.bind(this); + req.open("GET", this.props.url, true); + req.send(); + // $.ajax({ + // url: this.props.url, + // success: function(data) { + // this.setState({data: data}); + // }.bind(this) + // }); }, handleCommentSubmit: function(comment) { var comments = this.state.data; comments.push(comment); this.setState({data: comments}); - $.ajax({ - url: this.props.url, - type: 'POST', - data: comment, - success: function(data) { - this.setState({data: data}); - }.bind(this) - }); + var req = new XMLHttpRequest(); + req.onload = function(){ + if (req.readyState !== 4 || req.status !== 200) return; + this.setState({data: JSON.parse(req.responseText)}); + }.bind(this); + req.open("POST", this.props.url, true); + req.setRequestHeader("Content-type","application/json"); + req.send(JSON.stringify(comment)); + // $.ajax({ + // url: this.props.url, + // type: 'POST', + // data: comment, + // success: function(data) { + // this.setState({data: data}); + // }.bind(this) + // }); }, getInitialState: function() { return {data: []};