newhtml.html:14 Uncaught SyntaxError: Unexpected token ':'

时间:2021-01-25 00:38:35

标签: javascript html jquery json

我正在处理一个项目,但出现错误。我收到错误“newhtml.html:14 Uncaught SyntaxError: Unexpected token ':'” Chrome 在这一行中提到了这个问题: user.name: $('#name').val(); 我不知道错误到底在哪里!我认为一切都应该很好,但我不明白为什么 : 以某种方式抛弃了这个......有什么建议吗?

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
      <script type="text/javascript">
                  

              $(document).ready(function() {
                  var user={};
                $("#submit").click(function() {
                    user.name: $('#name').val();
                    user.email: $('#email').val();
                    user.password: $('#password').val();
                    user.confirmpassword: $('#confirmpassword').val();
                    var userObj = JSON.stringify(user);
        .ajax({
            type: "POST",
            url:  "http://localhost:8080/WebApplication6/webresources/generic/all",
            data: userObj,
            contentType: "application/json; charset=utf-8",
            success: function() 
            {
                alert('Success');
            },
            error: function(error) 
            {
                alert(error);
            }
        });
    });
});

    
    </script>


  
          
            <h2>Sign In</h2>
         

  
            <div class="col-lg-6 mt-5 mt-lg-0" data-aos="fade-left" data-aos-delay="100">
  
            
                <div class="form-row">
                    <div class="col-md-6 form-group">
                      <input type="text" name="name" class="form-control" id="name" placeholder="Username" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
                      <div class="validate"></div>
                    </div>
                    <div class="col-md-6 form-group">
                      <input type="email" class="form-control" name="email" id="email" placeholder="Email" data-rule="email" data-msg="Please enter a valid email" />
                      <div class="validate"></div>
                    </div>
                  </div>
                <div class="form-group">
                  <input type="password" class="form-control" name="password" id="password" placeholder="Password" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
                  <div class="validate"></div>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" name="confirmpassword" id="confirmpassword" placeholder="Confirm Password" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
                    <div class="validate"></div>
                  </div>
                <div class="text-center"><button name="submit" id="submit" >Sign In</button></div>
             
     
</body>
</html>

2 个答案:

答案 0 :(得分:0)

$(document).ready(function() {
                $("#submit").click(function() {
                    var user={name: $('#name').val(), email: $('#email').val(), password: $('#password').val(), confirmpassword: $('#confirmpassword').val() };
                    var userObj = JSON.stringify(user);
       $.ajax({
            type: "POST",
            url:  "http://localhost:8080/WebApplication6/webresources/generic/all",
            data: userObj,
            contentType: "application/json; charset=utf-8",
            success: function() 
            {
                alert('Success');
            },
            error: function(error) 
            {
                alert(error);
            }
        });
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>



  
          
            <h2>Sign In</h2>
         

  
            <div class="col-lg-6 mt-5 mt-lg-0" data-aos="fade-left" data-aos-delay="100">
  
            
                <div class="form-row">
                    <div class="col-md-6 form-group">
                      <input type="text" name="name" class="form-control" id="name" placeholder="Username" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
                      <div class="validate"></div>
                    </div>
                    <div class="col-md-6 form-group">
                      <input type="email" class="form-control" name="email" id="email" placeholder="Email" data-rule="email" data-msg="Please enter a valid email" />
                      <div class="validate"></div>
                    </div>
                  </div>
                <div class="form-group">
                  <input type="password" class="form-control" name="password" id="password" placeholder="Password" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
                  <div class="validate"></div>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" name="confirmpassword" id="confirmpassword" placeholder="Confirm Password" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
                    <div class="validate"></div>
                  </div>
                <div class="text-center"><button name="submit" id="submit" >Sign In</button></div>

答案 1 :(得分:0)

以上给出的答案有效,只是想解释您的误解。

如果您在定义 JSON 对象时分配属性,则可以像在代码中那样使用 :。例如:

var user = {
   "property1": "value1",
   "property2": $('#something').val()
};

如果您要为对象定义之外的本质变量赋值,则需要使用 =。例如:

var user = {};
user.property1 = "value1";
user.property2 = $('#something').val()

以上两个代码块的结果完全相同,但您可以看到何时使用 : 和何时使用 = 的不同。