Please enable Javascript to correctly display the contents on Dot Net Tricks!

Error code 12031 in Ajax enabled wcf

  Author : Shailendra Chauhan
Posted On : 25 Sep 2012
Total Views : 40,582   
Updated On : 23 Nov 2016

From last couple of days I was trying to call a ajax enabled wcf service method with the help of jquery. But each and every time I am getting the error code 12031 returned by the service. I did googling for this error but I was unable to find the solution. After spending much time on R&D, I found the reason why I wa geeting this error.

In this article, I am going to share my experience with you so that you will be able to resolve this error in ajax enabled wcf service. I have the below service and code for calling wcf.

WCF Service

 namespace WCFBAL
 public class Employee
 public int EmpID;
 public string Name;
 public int Salary;
 public DateTime JoiningDate;
 [ServiceContract(Namespace = "")]
 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
 public class WCFBALService
 [WebInvoke(Method = "POST")]
 public Employee GetEmpInfo(int mEmpID)
 //suppose you have a list of employee like as in the database
 List lst = new List() 
 new Employee { EmpID = 1, Name = "Deepak", Salary = 12000, JoiningDate = Convert.ToDateTime("11/05/2011") },
 new Employee { EmpID = 2, Name = "Mohan", Salary = 18000},
 new Employee { EmpID = 3, Name = "Mohan", JoiningDate = Convert.ToDateTime("06/10/2011") }
 var q = lst.Where(m => m.EmpID == mEmpID).FirstOrDefault();
 Employee mobjEmp = new Employee();
 if (q != null)
 mobjEmp.EmpID = q.EmpID;
 mobjEmp.Name = q.Name;
mobjEmp.Salary = q.Salary; // comment this line to expect same error 
 //The no error will be raised since "Salary" field is of int type and default value of int variable is "0", so zero value will be assigned to it. 
 mobjEmp.JoiningDate = q.JoiningDate; // comment this line and provide mEmpID=1 to raise error 
 //The error will be raised since "JoiningDate" field is not being serialized because this of DateTime and this has no default value and not assign any value to it.
 return mobjEmp;
 catch (Exception mex)
 return null;

WCF Service Method Calling Code

 <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript">
 $('#btn').live('click', function () {
 // debugger;
 var value = $('#txtEmpID').val();
 if (value != '') {
 var strJSONFilterCriteria = '{"mEmpID":"' + value + '"}';
 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8",
 url: '<%=ViewState["WCFBalUrl"]%>' + '/GetEmpInfo',
 data: strJSONFilterCriteria,
 dataType: "json",
 error: function (msg) {
 // debugger;
 alert('Service call failed: ' + msg.status + ' Type :' + msg.statusText);
 success: function (msg) {
 var str = "Emp ID:" + msg.d.EmpID + "\n";
 str += "Emp Name:" + msg.d.Name + "\n";
 str += "Emp Salary:" + msg.d.Salary + "\n";
 //str += "JoiningDate:" + new Date(parseInt(msg.d.JoiningDate.substr(6))) + "\n";
 } }); }
 return false;
 WCF Error Code 12031
 To learn more about Dot Net Tricks visit <a href="" title="Dot Net Tricks "></a>.
 Enter Employee ID (1-3)
 <asp:TextBox ID="txtEmpID" runat="server" ClientIDMode="Static"></asp:TextBox>
  <asp:Button ID="btn" runat="server" Text="Get Info" ClientIDMode="Static" />

I hope you will enjoy this trick while programming with Ajax enabled WCF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
20 JAN
ASP.NET Core with Angular4 (Online)
07:00 AM-09:00 AM IST / 07:30 PM Central Time
20 JAN
ASP.NET MVC with Angular (Classroom)
05:00 PM to 07:00 PM IST (+5:30 GMT)
20 JAN
ASP.NET MVC with Angular4 (Online)
03:00 pm to 05:00 pm
20 JAN
Angular2+4 with Firebase (Classroom)
03:00 PM to 05:00 PM IST (+5:30 GMT)
16 JAN
Developing Microsoft Azure Solutions (Online)
07:00 AM-09:00 AM IST/ 07:30 PM -09:30 PM CST
16 JAN
React with Redux (Online)
07:00 AM-08:30 AM IST
15 JAN
Big Data Hadoop Developer (Online)
05:00 PM to 06:30Pm IST / 06:30AM to 08:00AM CST
ASP.NET Core with Angular4 (Online)
09:00 Pm to 11:00 Pm IST (+5:30Gmt)
Big Data Hadoop Developer (Online)
07:00 AM - 08:30 AM IST(+5:30 GMT)
16 DEC
ASP.NET MVC with Angular4 (Classroom)
10:00 AM - 01::00 PM IST(+5:30 GMT)
12 DEC
ASP.NET MVC with Angular4 (Online)
09:00 PM - 11:00 PM IST(+5:30 GMT)
Developing Microsoft Azure Solutions (Online)
07:00 AM-09:00 AM IST / 08:30 PM Central Time
Angular2+4 with Firebase (Online)
10:30 AM-12:30 PM IST(+5.30 GMT)
27 NOV
ASP.NET Core (Online)
07:00 AM to 09:00 AM IST (+5:30 GMT)
11 NOV
ASP.NET MVC (Online)
08:00 AM-10:00 AM IST / 09:30 PM To 11:30 PM(CST)