简体   繁体   English

未捕获的语法错误:嵌入:意外的令牌

[英]Uncaught SyntaxError: embedded: Unexpected token

Given a React code:给定一个 React 代码:

 <script type="text/babel">
              var Var = React.createClass({
                render: function() {
                  return (
                    var args = {
                      "q": "data",
                      "offset": "0"
                    };

                    $.ajax({
                  ....headers and other stuff).done(function(JSONdata) {
                  console.log(JSONdata);

                }).fail(function() {
                  alert("You have an error");
                });
              );
            }
          });
          ReactDOM.render(<Var />, document.getElementById('div'));

Im getting error Uncaught SyntaxError: embedded: Unexpected token that shows the lines:我收到错误Uncaught SyntaxError: embedded: Unexpected token that shows the lines:

            render: function() {
              return (
                var args = {
                  "q": "data",
                  "offset": "0"
                };

I think it has to do with the curly brackets , but what is the right way to write them inside React module?我认为这与大括号有关,但是在 React 模块中编写它们的正确方法是什么? Or what is the actual error here ?或者这里的实际错误是什么?

I have babel and react src already included in my html file.我的 html 文件中已经包含了 babel 和 react src。 I need all of the key-values in args in order to send a request by ajax.我需要args中的所有键值才能通过 ajax 发送请求。

Solution for that is just wrap your return that just automatically execute it into a function解决方案只是将您的返回包装起来,它会自动将其执行到一个函数中

return (function(){
    var args = {
      "q": "data",
      "offset": "0"
    };

    $.ajax({
  ....headers and other stuff).done(function(JSONdata) {
    console.log(JSONdata);

    }).fail(function() {
      alert("You have an error");
    });
});

so your code will be like this所以你的代码会是这样的

var Var = React.createClass({
    render: function() {
        return (function(){
            var args = {
              "q": "data",
              "offset": "0"
            };

            $.ajax({
          ....headers and other stuff).done(function(JSONdata) {
            console.log(JSONdata);

            }).fail(function() {
              alert("You have an error");
            });
        })();
    }
});
ReactDOM.render(<Var />, document.getElementById('div'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM