title: How to post form data as JSON?
body: <p>I'm trying to build a registration site for a group project we are working on but can't figure out how to send the form data as json. I've tried googling a lot and changing the code but nothing seems to work. The problem I have is that when i press on the submit button I get an error like this from the API:</p>
  
  <blockquote>
    <p>{"":["The input was not valid."]}</p>
  </blockquote>
  
  <p>I think the reason is that my form does not send the data as JSON and it's they format they require according to their API documentation. My form code looks like this:</p>
  
  <pre><code>&lt;form id="register_form" action="https://https://url.com/users/register" method="post"&gt;
          &lt;input type="text" pattern="[A-Za-z]{1,20}" placeholder="Name" name="name" title="Up to 20 alphabetical characters" required&gt;
          &lt;input type="email" placeholder="Email" name="email" title="Must be a valid email address" required&gt;
          &lt;input type="password" pattern="[a-zA-Z0-9-]+{8,20}" placeholder="Password" name="password" title="Must be 8 or more characters long and contain at least one number and one uppercase letter" required&gt;
          &lt;input type="text" pattern="[a-zA-Z0-9-]+" placeholder="Homeadress" name="homeadress"&gt;
          &lt;input type="text" placeholder="Postnumber" name="postnumber"&gt;
          &lt;input type="text" placeholder="City" name="city"&gt;
          &lt;br&gt;
          &lt;button value="Submit" type="submit"&gt;Register&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And the script i've been trying to get to work looks like this:</p>
  
  <pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
    $('register_form').on('submit', function(event){
  
      var obj = $('register_form').serializeJSON();
  
      $.ajax({
          type: 'POST',
          url: 'https://url.com/users/register',
          dataType: 'json',
          data: JSON.stringify(obj),
          contentType : 'application/json',
          success: function(data) {
              alert(data)
          }
      });
  
     return false;
   });
  &lt;/script&gt;
  </code></pre>
  
  <p>Any help would be greatly appreciated since I'm not very familiar with coding stuff like this.</p>
  
  <p>Edit:</p>
  
  <p>I also tried it with a script like this but still getting the same response:</p>
  
  <p></p>
  
  <pre><code>&lt;script&gt;
  
  $(document).ready(function(){
  
      $("#submit").on('click', function(){
  
     var formData = {
  
          "name": $('input[name=name]').val(),
          "email": $('input[name=email]').val(),
          "password": $('input[name=password]').val(),
          "homeadress": $('input[name=homeadress]').val(),
          "postnumber": $('input[name=postnumber]').val(),
          "city": $('input[name=city]').val()
      };
  
         $.ajax({
          headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json'
          },
          url: 'https://url.com/users/register', 
          type : "POST", 
          dataType : 'json', 
          data : JSON.stringify(formData), 
          success : function(result) {
  
              console.log(result);
          },
          error: function(xhr, resp, text) {
              console.log(xhr, resp, text);
          }
      })
  });
  });
  </code></pre>
  
  <p></p>
  
  <p>I tested it with our teachers test api also and the response is this:</p>
  
  <blockquote>
    <p>{"message":"Bad Request","reason":"val: nil fails spec: :user-system.spec/login-request predicate: map?\n"}</p>
  </blockquote>