Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 596983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:13:51+00:00 2026-05-13T16:13:51+00:00

In my document I have this script: $.ajax({ type:POST,url:ajax.php,data:data, success: function() { //onsuccess },

  • 0

In my document I have this script:

$.ajax({ type:"POST",url:"ajax.php",data:data,
    success: function() { 
        //onsuccess
    },
    error: function() { 
        //onerror
    }
}); 

How can I, in the document ajax.php, 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?)

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 $.ajax. 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). But of course by doing it this way, I can’t separate the different fields from the other.

Based on what I want to use it for, can I somehow throw a “custom error”, so if the field name hasn’t been filled out properly, it returns the name of the field (i.e. name). Is it sufficient to do something like this:

In ajax.php:

if (isFilledOutProperly($name) == false) {
    echo "name";
}
else {
    echo "success";
}

And:

success: function(data) { 
    if (data == 'name') {
        //error
    }
            else {
        //success
    }
},

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:13:51+00:00Added an answer on May 13, 2026 at 4:13 pm

    The error clause of $.ajax() calls isn’t really for the circumstance you’re referring to. error covers cases of, say, HTTP errors. Validation failures aren’t really errors in this case. You’re better off indicating those in the success callback with appropriate data returned from the server.

    If you look at the documentation:

    error(XMLHttpRequest, textStatus, errorThrown)Function
    

    A function to be called if the request
    fails. The function is passed three
    arguments: The XMLHttpRequest object,
    a string describing the type of error
    that occurred and an optional
    exception object, if one occurred.
    Possible values for the second
    argument (besides null) are “timeout”,
    “error”, “notmodified” and
    “parsererror”. This is an Ajax
    Event
    .

    Note: the error conditions it talks about are parse errors, timeouts, errors (HTTP) and not modified (which is another HTTP response).

    Technically your request succeeds but the validation fails, which isn’t an error per se.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this jQuery-AJAX code below and a form: <script type=text/javascript> $(document).ready(function () {
I have this function working <script type=text/javascript> $(window).scroll(function() { if ($(window).scrollTop() == $(document).height() -
I have this code running great in ff, opera and chrome: <script type=text/javascript> $(document).ready(function(){
I mean I have this code: <script> $(document).ready( function() { $('html, body').animate({ scrollTop: $(.error).offset().top
I have (basically) this code - <script type=text/javascript language=javascript> $(document).ready(function() { $(#loadDiv).load(mypage.html, function(){ alert($(#someText).text())
I have a JS code like this <script type=text/javascript> $(document).ready(function(){ $('.main').on('click', '.block', function (){
I have the following within an XHTML document: <script type=text/javascript id=JSBALLOONS> function() { this.init
I have a code like this <script type=text/javascript> function email(){ var myTextField = document.getElementById('email_hidden').value;
I have this jQuery code: <script type=text/javascript> $(document).ready(function() { $('.vote_up').click(function() { alert ( test:
Say I have this code in my page: <script language=javascript> $(document).ready(function() { $(.test).click(function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.