2017-09-04 35 views
0
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); 
     }); 
} 

,但我得到:未捕获的SyntaxError:意外的标记<

Uncaught SyntaxError: Unexpected token < error...

我怎样才能解决这个问题?

+1

你会得到哪一行错误?我在上面的代码片段中没有看到任何“<”。 – Nisarg

+0

错误发生在'componentDidMount()'你必须写''function componentDidMount()' – adda82

+0

你还没有忘记将代码从JSX转换成JS? – Amid

回答

0

在你.babelrc文件

{ 
    "presets":[ 
     "react-app" 
    ] 
} 

然后 npm install --save-dev babel-preset-react-app

这是创建反应的应用程序使用,并主要更新以处理最新的语法。

相关问题