Hi Guys,
Is there any special settings in Kentico to render web service as JSON?
In my code which is in WebService.cs, I already declare ScriptMethod to response as JSON, but still header response as a XML.
I pasted my code as bellow
using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Data;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using CMS.CMSHelper;
using CMS.GlobalHelper;
/// <summary>
/// Empty web service template.
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
/// <summary>
/// Constructor.
/// </summary>
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
/// <summary>
/// Returns the data from DB.
/// </summary>
/// <param name="parameter">String parameter for sql command</param>
[WebMethod]
public DataSet GetDataSet(string parameter)
{
// INSERT YOUR WEB SERVICE CODE AND RETURN THE RESULTING DATASET
return null;
}
/// <summary>
/// The web service method to be called by AJAXControlToolkit. The signature of this method must match this method.
/// Note that you can replace "GetCompletionList" with a name of your choice, but the return type and parameter name and type must exactly match, including case.
/// </summary>
/// <param name="prefixText">Prefix to be searched</param>
/// <param name="count">Number of suggestions to be retrieved from the web service</param>
/// <returns>Array of suggestions</returns>
[WebMethod(MessageName = "GetCompletionList")]
[ScriptMethod]
public string[] GetCompletionList(string prefixText, int count)
{
// INSERT YOUR WEB SERVICE CODE AND RETURN THE RESULTING STRING ARRAY
return null;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetTreeNodeNames(string pathParameter)
{
if (string.IsNullOrEmpty(pathParameter))
throw new ArgumentException("No PathParameter specified");
int cacheMins = 30;
string cacheKeyName = "GetTreeNodeNames";
DataSet ds = null;
var treeNodes = new List<string>();
using (var cs = new CachedSection<DataSet>(ref ds, cacheMins, true, null, cacheKeyName, pathParameter))
{
if (cs.LoadData)
{
// Get from database
ds = TreeHelper.SelectNodes(pathParameter, false, "CMS.MenuItem");
CacheHelper.GetCacheDependency("touchthis".ToLower());
//cs.CacheDependency = ...
cs.Data = ds;
}
}
if (ds != null)
{
if (ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
treeNodes.Add(dr["NodeName"].ToString());
}
}
var jsr = new JavaScriptSerializer();
string output = jsr.Serialize(treeNodes);
return output;
}
}