Search code examples
asp.netjsonrequestwebmethodjsonserializer

How to resolve Error: Unexpected token '<' in Asp.Net request?


I am trying to call a WebService in my ASP. Net app and am getting the following error: Error: Unexpected token '<'. So far it seems like a json parsing issue, but I am not sure how to resolve it. In my app, I have a data manager that calls a stored procedure from a local SQL .mdf database. Essentially, its a public DataTable called GetEventsByState() and it is stored in my DataAcessManager.cs file. It is called from a WebService in my App_Service.asmx.cs file. The goal is to return the datatable as json in the WebService so I can then call it in my app.js file:

RII_Service.cs

    [WebMethod]
    public string GetListOfEventsByState()
    {
        _dtMgr = new DataAccessManager();
        //string state = st;
        /*HttpContext.Current.Response.Write(GetEventsData());*/
        DataTable EventsList = _dtMgr.GetEventsByState();
        var lst = EventsList.AsEnumerable()
            .Select(r => r.Table.Columns.Cast<DataColumn>()
                .Select(c => new KeyValuePair<string, object>(c.ColumnName, r[c.Ordinal])
                ).ToDictionary(z => z.Key, z => z.Value)
            ).ToList();
        //now serialize it
        try
        {
            string json = new JavaScriptSerializer().Serialize(lst);
            json = json.TrimEnd();
            return json;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

This WebMethod is then called in app.js on page load like this:

app.js

//Function to call Asp.Net Web Service and retrieve events list by state
var EventsData = esriRequest({
    url: "RII_Service.asmx/GetListOfEventsByState",
    content: {
    },
    dataType: "jsonp",
    handleAs: "json"
});
EventsData.then(
    function (response) {
        console.log(response);
        events_json = response;
....
}, function (error) {
    console.log("Error: ", error.message);
}); 

When I put break points on RII_Service.cs for the WebMethod GetListOfEventsByState(), it produces a viable json string that looks like this:

"[{\"FullEventName\":\"NJ Cindy 2005\",\"State\":\"NJ\"},{\"FullEventName\":\"NJ Gordon 2000\",\"State\":\"NJ\"}...]

However when it goes back to the EventsData request, it goes right to the error. Any clues or suggestions as to what I am doing wrong here.


Solution

  • This seemed to be an issue with the xml/html being pulled over to the app.js request when making a call to the WebMethod in RII_Service.cs. Despite GetListOfEventsByState() showing a json string being returned, the <string> tag would always get picked up on the app.js side (thus explaining the '<' error). To resolve this, I did 2 things. First, serialized it using the NewtonSoft.Json method JsonConvert

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    

    *You might have to install this using Tools > NuGet Package first. I then used System.Web.HttpContext to format the output. So, I reworked my WebMethod GetListOfEventsByState() as follows and it worked. Hope this helps anyone having similar issues:

        [WebMethod]
        //public string GetListOfEventsByState()
        public void GetListOfEventsByState()
        {
            _dtMgr = new DataAccessManager();
            DataTable EventsList = _dtMgr.GetEventsByState();
    
            //now serialize it
            try
            {
                string JSONresult;
                JSONresult = JsonConvert.SerializeObject(EventsList);
                HttpContext.Current.Response.Write(JSONresult);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }