How to handle PageRequestManager ParserError Exception?

  • Share/Save/Bookmark

Recently i was developing some AJAX enabled web page ( aspx) having update panel in it, almost entire development has been finished.  Suddenly i got this following error.

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near ‘<HTML><HEAD><TITLE><’.

So it’s always difficult to find fix to these type of errors, But some how i managed at the right time. so thought of sharing the solution & the way how i moved towards solution.

When i developed this AJAX ( using update panel) enabled web page, i tested thoroughly on my local environment but haven’t seen this error. so we gone ahead and deployed this to development environment, there this error thrown always when page got timed out. So i though definitely it should be some thing configuration settings of that machine or the development environment to deal with. So i thought of debugging the error, this is how i debug the error.

I have added the following code to my JavaScript, Then i have written the EndRequestHandler function. Make sure that you put that in window_onload() otherwise there is possibility that you may get ‘SYS’ undefined error.

   window.onload = window_onload;
   function window_onload()
   {
       Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
   }

Add end request to page request manager to handle the error, make sure that you have added this handler in window_onload() because there is possibility that you may get ‘sys’ undefined error if you declare openly.

    function EndRequestHandler(sender, args)
    {
      if (args.get_error() != undefined)
       {
           if (args.get_response().get_responseData().indexOf("<HTML><HEAD><TITLE>") == 0 )
           {
                args.set_errorHandled(true);
                __doPostBack("","");
           }
           else
           {
               // not my error so let the default behavior happen
           }
       }
    }

I have put the debugger in the above function, so got a chance to look at what args contain. What ever the response you got before rendering it will be there in args._response. so if you see what response you got exactly you will get to know from where this error came from. So debugged it and found that instead of xml response i got some HTML response which could not be parse by DOM object ( Ajax ).and also it gave me a hint that the page is about to redirect to login.fcc file.Then i understood the problem & found the solution very easily.

If you web application is enabled with SITE MINDER, When your session expires, Site Minder will create a redirect page on the fly and stream it to the browser that will basically try to redirect to the login page.  If you are in a AJAX call, you will get the standard "Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed" message.

so this is the case where i can not handle by code on server side, because this is site minder job to send the application page to login page which i can stop using my web page. So our objective is now to redirect the page to login page without giving the above alert. The way we can work around this is to catch the error and try to parse out the html that Site Minder creates and handle the redirect ourselves.  That is what i have done in the above code.

I just checked whether args.get_error() have some thing for me, if  there is some thing then check what is the error name, mostly it will be parse error exception. Then check the response you got, is that the response you expected if yes do normal work otherwise do what ever you want.

But parse exception may come in many scenario’s , this example is just one scenario out of many.  while doing analysis for fixing this one i have read few scenario’s when this error may come. I will also share those scenario’s with you after i explain what exactly the reason for getting this parse exception.

ParseErrorException Comes when some one / code has modified your page response which was not understandable by the Ajax parser ( DOM). To make sure that this error don’t come either you don’t modify the response or you modify it by making sure that it’s understandable to your parser.

Here are few come reasons why this ParseErrorException Comes

1. Response.write()

As i mentioned earlier, if you write something into response object which is not understandable to your parser, you will get the error. so either don’t use Response.write() or write in a format understandable to parser.

Eilon has listed few other reasons here. This article gives more reasons about the ParseErrorException.

2. if your web.config file doesn’t have the following line you may get this error.

<httpModules>

            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

        </httpModules>

 

3. There is chance that if enableEventValidation is set to true you may get this error. If you don’t find the exact reason try this one.

enableEventValidation=”false”

By putting the above to false, some what your security will get affected.

4. If some exception comes in our server side code which in turn will redirect the page to some xyz page, mostly you could see this error as html out put will have some not understandable code to parser. if this error comes when it’s requires to redirect to some page or some thing like that give a try there.

If  you could not solve your ParseExceptionMessage error, please use the above debugging technique and send me that args._Response, i will try to figure out the solution for you.

Categories : ASP.Net, Javascript

Comments

  1. [...] my Previous article ‘How to handle PageRequestManager ParserError Exception?’ i have explained the ways to debug this problem and handling or customizing the error message. [...]

Leave a Reply

About Techieon

Techieon is all about sharing Development experience of experienced talented developers of different technologies working in real time. Here at Techieon we talk about latest updates of different technologies, provide solutions to the developers problems, write tutorials on frequently used tools of development and provide tips to developers day to day activities.

This is a Widget Section

This section is widgetized. If you would like to add content to this section, you may do so by using the Widgets panel from within your WordPress Admin Dashboard. This Widget Section is called "Feature Bottom Middle"

Want to Write for Techieon?

Techieon have few opening positions for talented developers who want to share their development experience with Techieon users.If you are interested in writing at Techieon please Contact us or e-mail me for more information.