title: When using $.ajax in jQuery, how can I deliberately throw an error in the loaded document?
body: <p>In my document I have this script:</p>
  
  <pre><code>$.ajax({ type:"POST",url:"ajax.php",data:data,
      success: function() { 
          //onsuccess
      },
      error: function() { 
          //onerror
      }
  }); 
  </code></pre>
  
  <p>How can I, in the document <code>ajax.php</code>, deliberately throw an error? Is it sufficient to just throw an 400 HTTP Status Code or something? (And if so, is 400 the right one?)</p>
  
  <p>Why I'm asking is that I want to use this to submit a form to the server, and if a field isn't filled out properly I want to alert the user through the error parameter in <code>$.ajax</code>. I know I could do this using javascript in the first place, but I'm hoping I could do it this way (so I don't have to write the verification two times). <strong>But of course by doing it this way, I can't separate the different fields from the other.</strong></p>
  
  <p>Based on what I want to use it for, <strong>can I somehow throw a "custom error"</strong>, so if the field <code>name</code> hasn't been filled out properly, it returns the name of the field (i.e. <code>name</code>). Is it sufficient to do something like this:</p>
  
  <p>In ajax.php:</p>
  
  <pre><code>if (isFilledOutProperly($name) == false) {
      echo "name";
  }
  else {
      echo "success";
  }
  </code></pre>
  
  <p>And: </p>
  
  <pre><code>success: function(data) { 
      if (data == 'name') {
          //error
      }
              else {
          //success
      }
  },
  </code></pre>
  
  <p>I may be on the wrong track here, but if someone have a better suggestion on how I can do this, please tell me. Thanks!</p>