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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:39:19+00:00 2026-06-11T06:39:19+00:00

I am trying to handle errors in Ajax. For this, I am simply trying

  • 0

I am trying to handle errors in Ajax. For this, I am simply trying to reproduce this SO question in Symfony.

$.ajaxSetup({
    error: function(xhr){
        alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
    }
});

but I can’t figure out what the code in the controller would look like in Symfony2 to trigger header('HTTP/1.0 419 Custom Error');. Is it possible to attach a personal message with this, for example You are not allowed to delete this post. Do I need to send a JSON response too?

If anyone is familiar with this, I would really appreciate your help.

Many 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-06-11T06:39:20+00:00Added an answer on June 11, 2026 at 6:39 am

    In your action you can return a Symfony\Component\HttpFoundation\Response object and you can either use the setStatusCode method or the second constructor argument to set the HTTP status code. Of course if is also possible to return the content of the response as JSON (or XML) if you want to:

    public function ajaxAction()
    {
        $content = json_encode(array('message' => 'You are not allowed to delete this post'));
        return new Response($content, 419);
    }
    

    or

    public function ajaxAction()
    {
        $response = new Response();
        $response->setContent(json_encode(array('message' => 'You are not allowed to delete this post'));
        $response->setStatusCode(419);
        return $response;
    }
    

    Update: If you are using Symfony 2.1 you can return an instance of Symfony\Component\HttpFoundation\JsonResponse (Thanks to thecatontheflat for the hint). Using this class has the advantage that it will also send the correct Content-type header. For example:

    public function ajaxAction()
    {
        return new JsonResponse(array('message' => ''), 419);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to handle AJAX errors in a web application that uses jQuery. I'm
The url im trying to match is: http://domain/games/getdata/genres/ This request is made via ajax
I'm trying to handle a ViewExpiredException exception on an ajax call using primefaces 2.2.1
I've been trying to troubleshoot this, but can't find a good function that will
This is the response page that i get frmo my ajax request. Internal Server
Currently Im trying to do ajax request using jQuery Mobile. I`m trying to use
I have a login form that I am trying to handle with ajax. At
I'm working on this website, trying to call a function from a C# DLL
I'm trying to perform an AJAX-request in a view, the user gives some input
I'm trying handle bad json data when parsed through json_decode(). I'm using the following

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.