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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:44:31+00:00 2026-05-22T23:44:31+00:00

In my current project I’m querying PHP with normal HTTP-request but also via AJAX

  • 0

In my current project I’m querying PHP with normal HTTP-request but also via AJAX where I sometimes return JSON-formatted data and sometimes normal text. When an error occurs, in a normal request, you see the error-message and can do something about it. With AJAX-requests on the other hand you cannot be sure if you get an error or the data you want because it isn’t displayed directly — you just run into troubles because your app won’t work any more.

So I thought about an error-handling-system. My best idea is to send a 500 “Internal Server Error” header if an error occurs, so in my JS, when I send an AJAX-Request, I can simply check for errors that way and handle it.

Unfortunately PHP doesn’t send that kind of header (like when you have a parse-error, or something in your code goes wrong) natively.

Can you:

a) Tell PHP to send headers like 500 if an error occurs? Or
b) Use set_error_handler to send a 500-header and then call PHP regular error handling? Or
c) Can you, in any other way send error headers when a PHP error occurs?

  • 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-22T23:44:32+00:00Added an answer on May 22, 2026 at 11:44 pm

    i prefer using Exceptions for handling errors. First, you need to install error-to-exception error handler (http://php.net/manual/en/class.errorexception.php, example 1), then wrap your main application code in a try-catch block and send appropriate headers in the catch part. For example:

    try {
        $myApplication->run();
    } catch(Exception $e) {
       // log or otherwise register the error
       header('HTTP/1.1 500 Internal Server Error');
    }
    

    unfortunately, this doesn’t work with so-called “Fatal errors”, to handle these you have to use stupid tricks like this

    ob_start();
    
    register_shutdown_function(function() {
        $p = ob_get_contents();
        if(preg_match('~Fatal error~', $p))
            header('HTTP/1.0 500 Internal Server Error');
    });
    
    ...your code... 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)
For my current project, I need to request XML data over a tcp/ip socket
For a current project I am working I need to return an aggregate report
In my current project in PHP and MySQL, I have a database of user
In my current project, we got a rather unusual request(to me). The client wants
In my current project, I am using Memcache to store key-value pairs, but since
In the current project I would like to create my own hash function but
My current project uses NUnit for unit tests and to drive UATs written with
My current project involves deploying an upgraded .exe file that runs as a Windows
My current project involves writing Perl code inside a Solaris VMWare appliance (hosted on

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.