简体   繁体   English

使用表单标签时React.js'意外令牌'<

[英]React.js 'Unexpected token' < when using form tag

I've been making a web app using mern stack with babel, webpack, redux and semantic-ui-react. 我一直在使用带有babel,webpack,redux和语义UI反应的mern堆栈制作一个Web应用程序。

But I got an error saying 但是我说错了

"Unexpected token <" in bundle.js. bundle.js中的“意外令牌<”。

This error only occurs when I send a request clicking a button in form tag. 仅当我发送请求单击表单标记中的按钮时,才会发生此错误。 If I make the page without a form tag, it works fine without any error. 如果我制作的页面没有表单标签,则可以正常工作而不会出现任何错误。

This is my codes in React. 这是我在React中的代码。

handleUpload(title, contents, userId) {
  return this.props.createPostRequest(title, contents, userId).then(
    () => {
      if(this.props.post.status === 'SUCCESS') {
        alert('Your post is saved successfully.');
        browserHistory.push('/');
        return true;
      } else {
        alert('Save Fail: ' + this.props.post.failReason);
        return false;
      }
    }
  );
}

render() {
  return(
    <div className="Write">
      <br/>
      <br/>
      <Form>
        <Container text>
          <Form.Input label='Title' fluid name='title' placeholder='title'
            value={this.state.title} onChange={this.handleChange}>
            <input/>
          </Form.Input>
          <Form.TextArea rows='20' name='contents' placeholder='Write here!'
            value={this.state.contents} onChange={this.handleChange}>
            <textarea/>
          </Form.TextArea>
          <br/>
          <Button.Group>
            <Button color='orange' as={Link} to='/'>Cancel</Button>
            <Button.Or/>
            <Button positive onClick={this.handleUpload}>Save</Button>
          </Button.Group>
        </Container>
      </Form>
    </div>
  );
}

When I type letters and click the save button, I can see an alert message saying 当我键入字母并单击“保存”按钮时,我会看到一条警告消息,内容为

Your post is saved successfully.. 您的帖子已成功保存。

And also the data I put is saved in mongodb. 我输入的数据也保存在mongodb中。 But after I click ok, the url changes from 'localhost:3000/post/write ' to ' localhost:3000/post/write?title=blah&contents=blah '. 但是,单击“确定”后,URL从'localhost:3000/post/write ”更改为“ localhost:3000/post/write?title=blah&contents=blah ”。 blah in the url is what I put in input tags. 等等是我在输入标签中放的东西。 Then console says 然后控制台说

Unxpected token <. 意外令牌<。

But, if I don't use a form tag in above codes, it works fine, which I totally have no idea what's wrong about.. The Form tag is from semantic-ui-react. 但是,如果我在上面的代码中不使用form标签,它就可以正常工作,我完全不知道这是怎么回事。Form标签来自语义UI反应。 So I need it. 所以我需要。 If I don't use Form, it would work fine but I should give up the design provided from semantic-ui. 如果我不使用Form,它将很好用,但是我应该放弃语义UI提供的设计。

Is there anyone who knows about this? 有谁知道这个吗? I guess it's related to the HTTP POST in form tags that make trouble for react.js to understand bundle.js in index.html after the server-side handles the post request from that form tag. 我想这与表单标签中的HTTP POST有关,在服务器端处理来自该表单标签的发布请求之后,这使React.js难以理解index.html中的bundle.js。

TL;DR TL; DR

handleSubmit = (e) => e.preventDefault()

<Form onSubmit={this.handleSubmit} />

Why? 为什么?

By default, an HTML form will make GET request to the current URI on submit, see here . 默认情况下,HTML表单将在提交时向当前URI发出GET请求,请参见此处 Also by default, a button in a form will submit it: 同样默认情况下,表单中的按钮将提交它:

http://codepen.io/levithomason/pen/RpEwWP http://codepen.io/levithomason/pen/RpEwWP

<form onsubmit="alert('submitted!')">
  <button>I'll submit</button>
  <button>Me too</button>
</form>

What is happening is, React is rendering a <form /> with some <button /> s inside of it and when you click them, it is making a GET request to the current URI with the form data. 发生的事情是,React呈现了一个<form /> ,其中包含一些<button /> ,当您单击它们时,它正在使用表单数据向当前URI发出GET请求。

Going out on a limb, I bet your local server that is serving your app doesn't have a handler to accept this request. 我敢打赌,我敢打赌,正在为您的应用提供服务的本地服务器没有处理程序来接受此请求。 Going further, I bet it also has a fallback that responds with the index.html on unknown requests. 更进一步,我敢打赌,它还有一个回退功能,可以响应未知请求使用index.html响应。 This is commonplace for single page apps to allow the app's soft router to handle routing, rather than the server. 对于单页应用程序来说,允许应用程序的软路由器而不是服务器来处理路由是很平常的事。 This is probably causing your request for bundle.js to actually receive the index.html , hence the unexpected token. 这可能导致您对bundle.js的请求实际上接收到index.html ,因此出现了意外的令牌。

Since you've stated: 既然您已经说过:

...if I don't use a form tag in above codes, it works fine... ...如果我在上述代码中未使用表单标签,则可以正常工作...

Simply preventing the form submit should solve it for you. 只需阻止表单提交即可为您解决。

在中使用.map<div> 标签-react.js</div><div id="text_translate"><p> 我真的很感激任何帮助。 所以该项目是一个反应项目。</p><p> 这是我获取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的内容:获取请求</a></p><p>这是我从服务器响应的内容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我试了一下 Postman,看看我收到的是什么类型的 json。 这是它返回的内容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 测试</a></p><p>我遇到的问题是我无法 map 收到的数据。 一旦输入链接,我只想在首页上以一种很好的方式显示接收到的数据。</p><p> 是否可以将 map 放在 div 标签内? 或者还有另一种方法吗?</p><p> <em><strong>请注意该项目是作为功能组件编写的</strong></em></p></div> - Using .map in <div> tag - react.js

暂无
暂无

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

相关问题 SyntaxError:React.js中的意外令牌&lt; - SyntaxError: Unexpected token < in React.js componentDidMount 意外令牌错误 [React.js] - componentDidMount unexpected token error [React.js] React.js 给出了意外的令牌 - React.js gives Unexpected token React.js 语法错误:意外的令牌 - React.js SyntaxError: Unexpected token < 通过在React.js中创建文件上载来获取奇怪的意外令牌 - Getting a strange unexpected token with creating a file upload in React.js Radium for React.js内联样式意外的令牌错误? - Radium for React.js inline-styles unexpected token error? SyntaxError Unexpected token &lt; in JSON at position 0 in react.js API 导航 - SyntaxError Unexpected token < in JSON at position 0 in react.js API navigation React.js-babel-loader-意外令牌 - React.js - babel-loader - Unexpected token 在中使用.map<div> 标签-react.js</div><div id="text_translate"><p> 我真的很感激任何帮助。 所以该项目是一个反应项目。</p><p> 这是我获取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的内容:获取请求</a></p><p>这是我从服务器响应的内容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我试了一下 Postman,看看我收到的是什么类型的 json。 这是它返回的内容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 测试</a></p><p>我遇到的问题是我无法 map 收到的数据。 一旦输入链接,我只想在首页上以一种很好的方式显示接收到的数据。</p><p> 是否可以将 map 放在 div 标签内? 或者还有另一种方法吗?</p><p> <em><strong>请注意该项目是作为功能组件编写的</strong></em></p></div> - Using .map in <div> tag - react.js 意外的字符 &#39;⇒&#39; React.js - Unexpected character '⇒' React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM