Results 1 to 10 of 10

Thread: How do you deal with error 500 responses to Ajax requests?

  1. #1
    Sencha User
    Join Date
    Nov 2010
    Posts
    85

    Default How do you deal with error 500 responses to Ajax requests?

    I have an ext form that I am submitting to my server. Due to a bug in my server code, an exception is being thrown and my web server's Error 500 page is being returned. The problem with this is my ext form is expecting a JSON response, but the Error 500 page is HTML, thus causing Ext to fail being unable to parse the '<' character.

    How can I handle this situation? I want to display a message to the user that an error occurred, but this Ext exception is causing my failure handler not being call, as well as the form's waitMsg to not go away.

  2. #2
    Sencha User
    Join Date
    Jan 2009
    Location
    Weinstadt
    Posts
    23

    Default

    I handle it in my SUCCESS-method of the request:
    PHP Code:
    loadFormSuccess:function(formresult)
    {
            
    console.log('-->'result.response.status);


  3. #3
    Sencha User
    Join Date
    Nov 2010
    Posts
    85

    Default

    Quote Originally Posted by Graveworm View Post
    I handle it in my SUCCESS-method of the request:
    PHP Code:
    loadFormSuccess:function(formresult)
    {
            
    console.log('-->'result.response.status);

    I don't understand. The 500 response and/or the JSon parsing exception (probably the latter) is causing the success or failure handlers from being called.

  4. #4
    Sencha User
    Join Date
    Nov 2008
    Location
    San Diego, Peoples' Republic of California
    Posts
    2,061

    Default

    Fix the 500 error and it's not a problem?

    Seriously though, it's something that's been discussed in other threads, that ExtJS might be able to catch error pages and throw up a dialog or something.

    In the meantime, here's some code I wrote that is a simple approach:

    http://code.google.com/p/helma-commu...nt/Core/rpc.js

    You basically try/catch around everything on the server side and send back JSON with an indication an exception occurred on the server and whatever you want to show on the client. In my case, I'm showing the source code file with the line the exception occurred highlighted.

    Another approach is based upon this page:
    http://dev.sencha.com/deploy/dev/docs/?class=Ext.Ajax

    Basically, you add beforerequest, requestcomplete, and requestexception handlers to Ext.Ajax (it's a singleton). In before request, you can add annotations (fields, members, whatever you want to call them) to the options object. In requestcomplete you can look at the response.status / response.responseText and examine it to see if it looks like an ASP/JSP/PHP/whatever error page (such as a compile error) and handle the error as you see fit.

    The annotations I mentioned might be a flag, "this is a file upload and thus not true Ajax" so you don't see some JSON inside a <textarea> tag and think it's an error page.

  5. #5
    Sencha User
    Join Date
    Nov 2010
    Posts
    85

    Default

    Haha I do intend to fix the 500 error :P.

    Your response did give me an idea, and after doing some research I think I found out how to return a JSON formatted 500 response for Ajax requests, which does solve the issue. Thanks!!

  6. #6
    Sencha User
    Join Date
    Sep 2009
    Posts
    112

    Default

    Here is what I did to handle my 403 errors. You can probably apply for your 500 errors. Use the requestexception of Ext.Ajax. You only need to set this up once in your whole application

    Code:
     Ext.Ajax.on('requestexception', function(con, res, opt){    	    	    	
        	if(res.status =='403')
        		window.location= '/login';
        }, this);
    In your case you could do

    Code:
    	 Ext.Ajax.on('requestexception', function(con, res, opt){    	    	    	
        	if(res.status =='500')
        		//show error msg
        }, this);

  7. #7
    Sencha User
    Join Date
    Nov 2010
    Posts
    85

    Default

    As an FYI, that didn't work with a error 500 occurring during form submission

  8. #8
    Sencha User
    Join Date
    Sep 2009
    Posts
    112

    Default

    Yes you are right. Only works with Ajax requests using the Ajax object

  9. #9
    Sencha User
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    Normal FormPanel submits also use Ext.Ajax, unless you are using standardSubmit:true or isUpload:true.

    For isUpload:true you can't detect the HTTP error status, so the error code must be passed in the response page.

  10. #10
    Sencha User
    Join Date
    Oct 2010
    Posts
    157

    Default

    so, you have an answer from the server ? yes ? if yes, your problem is only that the returned text is some complex text that cannot fecth ok in the json response ? if only this is the problem, why you don't return some clean answer, like
    {"success":"false","response":"yourVeryOwnExplanationAbout500Error"}

    1. you will have a reponse page (that Condor suggest for such cases when cannot detect some "automatic" errors)

    2. you got the answer at the line 12 (give as example by mschwartz - http://code.google.com/p/helma-commu...nt/Core/rpc.js).

Similar Threads

  1. Handling 500 internal server error
    By Maradonut in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 4 Oct 2013, 12:48 AM
  2. Managing HTTP Requests on Stores, TreeLoaders and Ajax Requests
    By j-joey in forum Ext 3.x: Help & Discussion
    Replies: 2
    Last Post: 14 Jun 2010, 11:32 PM
  3. [CLOSED][3.0rc1.1] 500 Error, Ext.Ajax issue
    By mschwartz in forum Ext 3.x: Bugs
    Replies: 2
    Last Post: 18 Jun 2009, 6:16 PM
  4. Displaying Catalyst Error AJAX Responses
    By jamie.nicholson in forum Community Discussion
    Replies: 1
    Last Post: 10 Aug 2008, 4:18 PM
  5. 500 SERVER ERROR
    By jeswa in forum Ext 1.x: Help & Discussion
    Replies: 1
    Last Post: 20 Jan 2008, 1:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •