"Sys.WebForms.PageRequestManagerServerErrorException: status code: 500" - javascript

I am using an asp.net text box inside ajax update panel. If I enter &# in the textbox and press Save Button , it gives a javascript error
Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500
Please help me why this error appears?

Probably ASP.NET Request Validation kicked in and detected a potentially dangerous request (the &# in the textbox value). This causes an HttpRequestValidationException to be thrown - hence the 500 HTTP code is returned by the UpdatePanel. The way I see it there are two ways to solve this problem:
Validate the contents of the textbox and replace any potentially dangerous (HTML like) values.
Disable request validation:
<%# Page ValidateRequest="false" %>
If you choose to disable request validation make sure that the value of this textbox is not output verbatim somewhere else in your application. Make sure you are using HttpUtility.HtmlEncode when displaying it in order to avoid XSS issues.

Related

Is there any better way to do like history.back() in django backend?

In Django, I have "form view(GET)" and "action view(POST)"
If there is a validation error or any other error in action view,
I want browser to go back to the previous form page with the input data whote by the user remains.
I tried:
At first, I used return redirect(request.META.get('HTTP_REFERER')).
This code goes back to the previous page well, but all the input fields became empty.
And now I am using return HttpResponse("<script>history.back();</script>") this code.
This works very well. but It looks weird sending javascript response.
What I expected was like return redirect.back()(with like HTTP 301 or 30X status code)
I was wondering if there is any better way or a proper way like officially provided by Django.

Overriding default PHP error messages with JavaScript

When adding more than 20 attachments to a contact form file input; PHP returns the following message:
Warning: Maximum number of allowable file uploads has been exceeded in Unknown on line 0
I can't replace this message with my own because it happens before my PHP script executes. Although If I get the returned warning and parse it with JavaScript, I can replace it with my own simpler warning message. Is there such a library that does this sort of thing with many warning and error messages that can't be changed with PHP?
You have to increase the max_file_uploads setting in php.ini, there is no way around it, if you want to use the code you have.
Or do something different, like submit the files in separate requests using AJAX.
Instead of styling the error message generated by PHP, you want to prevent it ever getting to that point. You need to implement validation before your AJAX gets submitted, so if your server only allows x file uploads at one time do:
Jquery:
if($("input:file")[0].files.length>x) {
// perform your ajax request
} else {
// show your error message
}

Django - show result (error) with javascript

I have form, that accepts number between 1-6 and a file. You can see code in my other question: https://stackoverflow.com/questions/43611769/django-javascript-modal-form-validation
I have to validate form, without having to reload it as it is modal form and it closes on refresh (or is should open it on refresh, but i don't really like the idea of reloading). So i want the modal to show error, if there is any (wrong file type, model already exists...) How would i achieve this?
Somebody suggested me to post form asynchronously and show error with javascript. How would i send javascript message from views in django, without the page having to reload? Could it be done with Json? How?
Thank you in advance!
You have to options:
Django validation view which will return JsonResponse
In that case you send your data to this view which will check if data are valid and return appropriate response. Then you parse response and show error if any.
Javascript validation. You make validator in your javascript code which will check if filetype is suitable etc. Here you have example how to allow only specified types of files.
In both cases you should validate your data in view in which you are saving to Database/server while someone could send data directly to your saving view(e.g. with Postman).
I would suggest second approach because it is faster and one less call to server.

Failing to catch ASP.Net Server error in sharepoint

I have an asp.net control that contains a grid view sitting on top of an Ajax update panel. The control has been added to a sharepoint page. When a button is clicked, some server code is called to store the contents of the grid.
If the server code throws an error, I want to spit out a javascript alert displaying the error message, but for some reason the error that bubbles up from the server is the generic 500 server error, which doesn't contain any details of the original error.
Can anyone explain why this is?
Many thanks
Gerry
The default behaviour of ASP.NET (and SharePoint) is to display a generic error page when an exception is thrown. For security reasons, this page hides all details of the error. With debug settings in web.config you can get a stack trace, but that won't give you something easily manipulated in javascript.
To return a custom error page you can either set up a global unhandled exception handler (probably overkill for this) or you can catch the exception and modify the response appropriately. For a rest method you would clear the response, set status to 500 and write the error message to the response. For an UpdatePanel, you would render the panel as an error message or with script to show the alert. Not sure if you would set status in this case - UpdatePanel is an ugly hack to fake true ajax in webforms, so you can't expect the code to be particularly clean.

ASP.NET Ajax Error: Sys.WebForms.PageRequestManagerParserErrorException

My website has been giving me intermittent errors when trying to perform any Ajax activities. The message I get is
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 '
<!DOCTYPE html P'.
So its obviously some sort of server timeout or the server's just returning back mangled garbage. This generally, unfortunately not always, happe
There is an excellent blog entry by Eilon Lipton. It contains of lot of tips on how to avoid this error:
Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it
Read the comments too. There is a comment of somebody with the same problem: "I solved it changing server idle time of my app pool on IIS. It was only 5, so I incremented it and now works."
"The UpdatePanel control uses asynchronous postbacks to control which parts of the page get rendered. It does this using a whole bunch of JavaScript on the client and a whole bunch of C# on the server.
Asynchronous postbacks are exactly the same as regular postbacks except for one important thing: the rendering. Asynchronous postbacks go through the same life cycles events as regular pages (this is a question I get asked often).
Only at the render phase do things get different. We capture the rendering of only the UpdatePanels that we care about and send it down to the client using a special format. In addition, we send out some other pieces of information, such as the page title, hidden form values, the form action URL, and lists of scripts."
Most common reasons for that error:
Calls to Response.Write():
Response filters
HttpModules
Server trace is enabled
Calls to Server.Transfer()
Probably there is an error occuring on post back. In this case, you can view the details about the error by adding a PostBackTrigger to your updatepanel and referencing the button which causes the problem:
<asp:updatepanel ID="updatepanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="button1" />
</Triggers>
<ContentTemplate>
</ContentTemplate>
</asp:updatepanel>
I had this happen to me and none of the causes on the list in the answer applied. I didn't find the root of the problem until I disabled my AJAX altogether. Discovered that the code was saving an object to the ViewState that contained an unserializable object. I made the object serializable and it started working again.
Problem: Sys.WebForms.PageRequestManagerParserErrorException will occur when redirecting your page, lets say button click inside UpdatePanel in aspxAjax.
SOlution:
Add a "GoTo" button in your aspx page where update panel is using and add it outside Update panel
In your code assign ur just registered userID to session variable , say Session["UseridJustregistered"]=Id from DB or UsernameField
Respose.Redirect("regSucces.aspx?urlid='" + Session["UseridJustregistered"] + "'");
Check if Session["UseridJustregistered"] is null or not
This is OLD Classic ASP way which can solve our problem , by the time Microsoft find a solution we can tackle it this way.
I solved this exact same problem removing the Content-Type: form the Custom HTTP Headers section in the HTTP Headers tab in IIS. This was breaking the encoding of the page and somehow it affected Ajax in general.
The Content-Type I had configured in IIS was setting the encoding to ISO-8859-1.
This may be a little hacky, but it solved the issue for me. I didn't have any of the common reasons for the error, so I just put in this band-aid in the page load:
if (Session.SessionID == "")
{
Page.Session.Add("SessionID", Session.SessionID);
}
I solved this same problem by removing mistakenly-nested UpdatePanels.
I finally solved my variant of this same problem. I was attempting to copy/move a selected value between 2 listboxes in a webform. In my case, I had to specifically call {listbox}.ClearSelection() prior performing the action the 2nd time around.
So obviously this problem/error message can occur for a multitude of reasons.
Change of the app pool FROM INTEGRATED to asp.net classic solved the problem for me.
In our case the issue was caused by a rewriting proxy on the way. The rewrite modified the content of the update panel response. But this response also contains original size. The rewriting mechanism cannot know that few bytes of the response actually contains original response size and it should also be modified.
The update panel response starts like this:
1|#||4|30502|updatePanel|pnlUpdate| ...
The 30502 is original size of the content which is being updated. Rewriting engine modifies the output, but the size stays unchanged => parser error exception.
I don't see a way how to overcome this issue from the client side. We would need to know how exactly was the content modified and then somehow change the size in the response before UpdatePanel ClientScript starts processing it.
I also got this error. The solution reported by "user1097991" solved it for a while (I was using not-serialized objects on viewstate)
But later the error returned again, now in a random fashion. After some search I got the answer: the viewstate was becoming too large and was been truncated. I disable some viewstates on grids and menus and the problem haven't shown again.
I found that my issue was related to a nul character being rendered in the databinding of a GridView. The expected length of the response wasn't matching the actual length of the response text which resulted in the error being thrown. Once I fixed the data in the database, I no longer got the error. The ultimate fix will be to sanitize the text getting rendered during the RowDataBound event.
Looking through the database, I couldn't see the bad data since SQL Server 2008 doesn't show the text if the nul character (Char(0)) is in the string. In the RowDataBound event of my GridView, I added code to throw an exception for any text that had special characters in it. This is how I found the record that contained the nul characters.
tl;dr - Check for nul characters in the rendered html.
Please also be aware that this can be caused by not properly html encoding what you may be rendering to the page through partial postbacks.
I had exactly the same error.
For me it was
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
Missing in the httpModules section of web.config (.Net 3.5 app)
This error seems to may be related to many various things.
Update the ScriptMode to "Release"
<asp:ScriptManager ID="ScriptManager" runat="server" ScriptMode="Release"></asp:ScriptManager>

Categories