Ajax - 500 Internal Server Error


I am trying to learn AJAX for this project at work. I have a site that loads medications that a patient is taking.

I call this AJAX function up recursively so that it will append a new table containing a single medication and 7 days worth of history. I am having issues getting the code to execute in FF and IE. Works perfectly fine in chrome. I had alerts displaying the xmlhttp.status and this is what I got:

xmlhttp.status==500 (internal server error).

I commented out all of my recursion so it is narrowed down to this tid bit of code. ( x keeps track of the number of meds so i know when to stop)

function LoadMeds()

  if ( x == MaxMedCount )
      document.getElementById("the_day").value = parseInt(document.getElementById("the_day").value)+7; 
  if ( x == (MaxMedCount - 1) )
      document.getElementById("x").value = x + 1;

      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
      {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                  var div = document.createElement('div');
                  div.innerHTML= xmlhttp.responseText;
                  document.getElementById("the_med_").value = the_med_;


      }"GET","URL with variables passed",true);
      document.getElementById("x").value = x + 1;

if more code is needed just let me know.

11/30/2016 4:15:43 PM

Accepted Answer

The 500 (internal server error) means something went wrong on the server's side. It could be several things, but I would start by verifying that the URL and parameters are correct. Also, make sure that whatever handles the request is expecting the request as a GET and not a POST.

One useful way to learn more about what's going on is to use a tool like Fiddler which will let you watch all HTTP requests and responses so you can see exactly what you're sending and the server is responding with.

If you don't have a compelling reason to write your own Ajax code, you would be far better off using a library that handles the Ajax interactions for you. jQuery is one option.

1/25/2011 4:13:37 AM

I think your return string data is very long. so the JSON format has been corrupted. You should change the max size for JSON data in this way :

Open the Web.Config file and paste these lines into the configuration section

      <jsonSerialization maxJsonLength="50000000"/>

This may be an incorrect parameter to your SOAP call; look at the format of the parameter(s) in the 'data:' json section - this is the payload you are passing over - parameter and data wrapped in JSON format.

Google Chrome's debugging toolbar has some good tools to verify parameters and look at error messages - for example, start with the Console tab and click on the URL which errors or click on the network tab. You will want to view the message's headers, response etc...


Uncomment the following line : [System.Web.Script.Services.ScriptService]

Service will start working fine.

[WebService(Namespace = "")]

 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.


public class WebService : System.Web.Services.WebService 

Had the very same problem, then I remembered that for security reasons ASP doesn't expose the entire error or stack trace when accessing your site/service remotely, same as not being able to test a .asmx web service remotely, so I remoted into the sever and monitored my dev tools, and only then did I get the notorious message "Could not load file or assembly 'Newtonsoft.Json, Version=, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dep...".

So log on the server and debug from there.


One must use behavior: JsonRequestBehavior.AllowGet in Post Json Return in C#


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]