jQWidgets Forums

jQuery UI Widgets Forums Plugins Data Adapter error with dataAdapter using .asmx ASP .NET json data

This topic contains 2 replies, has 2 voices, and was last updated by  cflowers 9 years, 7 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author

  • cflowers
    Participant

    Getting error

    
    
    
    SyntaxError: Unexpected token <<?xml version= "1.0" encoding="utf-8?>>][{"Name":"about"},{"Name":"about2"}... more data...}]</anyType>
    

    I’m trying to get autocomplete to work with jqxInput and jqx.DataAdapter. Here is my code:

    Reports.asmx

            [WebMethod(EnableSession = true)]
            [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
            public object GetIntellisense(string pattern )
            {
                try
                {
                    using (var proxy = GTS.Client.GTSWebServiceFactory.CreateReportProxy())
                    {
                        List<Intellisense> intellisenseList = new List<Intellisense>();
                        List<string> names = proxy.GetReportIntellisense("DocumentActivity", "DocumentSearch", pattern + "%");
                        for (int i = 0; i < names.Count; i++)
                        {
                            Intellisense x = new Intellisense();
                            x.Name = names[i];
                            intellisenseList.Add(x);
                        }
                        proxy.Close();
                        JavaScriptSerializer js = new JavaScriptSerializer();
                        string json = js.Serialize(intellisenseList);
                        return json;  // getting here with good json data
                    }
                }
                catch (Exception exc)
                {
                    GTS.Logging.Log.Warn("error getting intellisense", exc);
                    throw new Exception(exc.Message);
                }
            }


    in DocumentActivity.aspx

    <input id="documentName" />

     $(document).ready(function () {
    $("#documentName").jqxInput({
                    source: function (pattern, response) {
                        var dataAdapter = new $.jqx.dataAdapter (
                            {
                                    type: "GET",
                                    //contentType: "application/json; charset=utf-8", // if this is uncommented then GetIntellisense is not called
                                    async: false,
                                    datatype: "json",
                                    data: "{ 'pattern': '" + pattern + "' }",
                                    url: 'Reports.asmx/GetIntellisense',
                                    datafields: [
                                    	{ name: 'Name', type: 'string' }
                                    ],
                                    cache: false,
                                    root: 'data'
    
                                },
                                {
                                    autoBind: true,
                                    formatData: function (data) {
                                        data.pattern = pattern;
                                        return data;
                                    },
                                    downloadComplet: function(data, textStatus, jqXHR) {                
                                        return data.d;            
                                    },
                                    loadError: function (xhr, status, error) {
                                        alert(error + xhr.responseText);
                                    }
                                }
                        )},
                    placeHolder: "Enter a document name",
                    height: 25,
                    width: 200,
                    minLength: 1    // MINIMUM 1 CHARACTER TO START WITH.
                });

    Not sure why the json data is coming back with header information. Can you advise?
    Thanks in advance.


    Peter Stoev
    Keymaster

    Hi cflowers,

    If your JSON contains: “< ” then it should be expected that there is an exception because this is not JSON, but XML.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com


    cflowers
    Participant

    For anyone looking to see how I solved this issue:

           [WebMethod(EnableSession = true)]
            [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
            public object GetIntellisense(/*string corrkey, string reportName, string paramName,*/ string pattern) 
            {
                try
                {
                    Context.Response.Clear();
                    Context.Response.ContentType = "application/json";
                    using (var proxy = GTS.Client.GTSWebServiceFactory.CreateReportProxy())
                    {
                        List<Intellisense> intellisenseList = new List<Intellisense>();
                        List<string> names = proxy.GetReportIntellisense("DocumentActivity", "DocumentSearch", pattern + "%");
                        for (int i = 0; i < names.Count; i++)
                        {
                            Intellisense x = new Intellisense();
                            x.Name = names[i];
                            intellisenseList.Add(x);
                        }
                        proxy.Close();
                        JavaScriptSerializer js = new JavaScriptSerializer();
                        string json = js.Serialize(intellisenseList);
                        object oJson = js.DeserializeObject(json); // needed to deserialize object before returning.
                        return oJson;
                    }
                }
                catch (Exception exc)
                {
                    GTS.Logging.Log.Warn("error getting intellisense", exc);
                    throw new Exception(exc.Message);
                }
            }
     $("#documentName").jqxInput({
                    source: function (pattern, response) {
                        var dataAdapter = new $.jqx.dataAdapter
    	                (
    	                    {
    	                        type: 'POST',
    	                        dataType: 'json',
    	                        async: true,
    	                        url: "Reports.asmx/GetIntellisense",
                                datafields: [
                            	    { name: 'Name', type: 'string' }
                                ],
                               
                                cache: false,
                                contentType: 'application/json; charset=utf-8',
    	                    },
    	                    {
    	                        autoBind: true,
    	                        formatData: function (data) {
    	                            data.pattern = pattern;  // make sure name is same as paramater
    	                            return JSON.stringify(data); // Needed to stringify data
    	                        },
    	                        loadComplete: function (data) {
    	                            if (data.d.length > 0) {
    	                                response($.map(data.d, function (item) {
    	                                    return item.Name
    	                                }));
    	                            }
    	                        },
    	                        loadError: function (xhr, status, error) {
    	                            alert(error + xhr.responseText);
    	                        }
    	                    }
    	                );
    
                    },
    
                    placeHolder: "Enter a document name",
                    height: 25,
                    width: 200,
                    theme: theme,
                    minLength: 1    // MINIMUM 1 CHARACTER TO START WITH.
                });
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.