【发布时间】:2018-02-12 07:22:52
【问题描述】:
jsonp = (url, callback) => {
var callbackName = 'jsonp_callback_' + Math.round(100000 * Math.random());
window[callbackName] = function(data) {
delete window[callbackName];
document.body.removeChild(script);
callback(data);
};
var script = document.createElement('script');
script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + callbackName;
document.body.appendChild(script);
}
componentDidMount() {
this.jsonp('https://www.naver.com', function(data) {
alert(data.meta.description);
});
}
但我明白了:
Uncaught SyntaxError: Unexpected token
我该如何解决这个问题?
【问题讨论】:
-
您在哪一行得到错误?我在上面的 sn-p 中没有看到任何
<。 -
错误出现在'componentDidMount()' 你必须写'function componentDidMount()'
-
你没有忘记将代码从 JSX 转译成 JS?
-
我替换了代码 omponentDidMount() { this.jsonp('naver.com'); } 但同样的错误发生...
-
你是否在你的 babel 配置中启用了
ES Class Fields & Static Properties(目前是第 3 阶段)?
标签: javascript ajax reactjs jsonp