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 6997649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:18:36+00:00 2026-05-27T20:18:36+00:00

I have read a few posts on fail parameters for a JQuery Ajax call,

  • 0

I have read a few posts on fail parameters for a JQuery Ajax call, but none that have directly answered my question. If you want to read up on my jumping off point here, this post would be a good start:

jquery: is there a fail handler for $.post in Jquery?

My problem is that there are a handful of things that may cause my application to fail – if my script returns false the above method would work just fine for me (if I understand it correctly), but most of the time my script will fail by kicking out an exception that I handle using the Zend Framework. I would prefer to return the exception so that I can provide the user with a more detailed message. Is it possible to have my PHP script return a value while still letting the Ajax call know that it was a failure?

  • 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-27T20:18:37+00:00Added an answer on May 27, 2026 at 8:18 pm

    Sure you can. First of all you need to categorize you errors, for example:

    • Fatals
    • Exceptions
    • false / error status

    I would advise you to take as a return value for correct and with no errors processing – 0. In all other case that would be an error.

    Another useful advise would be to use JSON as a client-server conversation.

    In PHP it would be:

    function prepareJSONResponse($code, $message, array $extra = array())
    {
        return json_encode(array_merge(
            $extra, array(
                'code'    => (int) $code,
                'message' => $message)));
    }
    

    In this case you could pass error code and message, and additional params in $extra, for example, for this call:

    prepareJSONResponse(1, 'Not enough data passed', array('debug' => true));
    

    Response from server side would be:

    {code:1,message:'Not enough data passed','debug': true}
    

    For the client side you need a wrapper function for $.ajax:

    // calback(result, error);
    function call(url, params, callback)
    {
        if (typeof params == 'undefined') {
            params = {};    
        }
    
        $.ajax({
            'type'      : "POST",
            'url'       : url,
            'async'     : true,
            'data'      : params,
            'complete'  : function(xhr) {
                if (xhr.status != 200) {
                    if (typeof callback == 'function') {
                        callback(xhr.responseText, true);
                    }
                } else {
                    if (typeof callback == 'function') {
                        callback(xhr.responseText, false);
                    }
                }
            }
        });
    }
    

    and function to validate JSON, in order if the corrupted format comes.

    function toJSON(data){
        try {
            data = JSON.parse(data);
        } catch (err) {
            data = { 'code' : -999, 'message' : 'Error while processing response' };
        }
    
        if (typeof data.debug != 'undefined') {
            console.log(data.debug);
        }
    
        return data;
    }
    

    Wrap in try-catch you code, and in catch statement do something like:

    try {
        ...
    } catch (Exception $e) {
        exit(prepareJSONResponse(1, $e->getMessage(), array(
            'debug' => 'There was an error while I were processing your request')));
    }
    

    The result would be that you receive debug information in browser console, and could handle errors / exception (prepareJSONResponse()) and fatals (by reading HTTP-status header, if it’s not 200, then there was error).

    Hope thats what you asked about.

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

Sidebar

Related Questions

I have read in many posts that global variables are bad, but I need
I've read through quite a few of posts, but none seem to do just
I have read a few posts about what I need but I can't manage
I have read a few posts but cannot figure out what is wrong.My Code
I've read quite a few posts that are very similar to the question I'm
I've read a few posts where people have stated (not suggested, not discussed, not
I'm fairly new to working with relational databases, but have read a few books
I have read quite a few selcet+update questions in here but cannot understand how
I have read quite a few posts on SO and around the web of
I have read a few posts about people putting in either their own icons

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.