react发起ajax get请求的参数问题

 deleteComment(id) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', this.props.deleteUrl + id, true);
        xhr.onload = function () {
            var deleted = xhr.responseText;
            if (deleted == 'true') {
                var index = -1;
                this.state.comments.map(function (c, idx) {
                    if (c.id == id) {
                        index = idx;
                    }
                })

                this.state.comments.splice(index, 1);
                this.setState({ comments: this.state.comments });
            }
        }.bind(this);
        xhr.send();
    }
    ReactDOM.render(
    <CommetBox url="/comments" submitUrl="/comments/new" deleteUrl="/comments/delete/" />,
    document.getElementById('content')
)

发起ajax的get请求,xhr.open('get', this.props.deleteUrl + id, true);在这里我是拼接url的,那个id就是要删除的元素的id,请问还有更优雅的方式调用吗?我指的是不拼接url

阅读 5.8k
1 个回答

用superagent,对promise进行的封装

npm install superagent --save

import request from 'supreagent'

request
.get('http://localhost:8080/user/ap...')
.then((res) => {

console.log(JSON.parse(res.text));

})
.then((res) => {
})
.catch((err) => {

console.log(err);

});

推荐问题