Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a JSON WebService in c# ASP.Net with a valid JSON output and query with JQuery/Ajax
    primarykey
    data
    text
    <p>I´ve created a <strong>c# ASP.Net JSON WebService</strong>. But I have big troubles with reading the data from this WebService because in my eyes the output is not a valid JSON format?</p> <p>Normally you should get a [object, object] back as data. But I get [object, document] What I am doing wrong or what I am missing?</p> <p><strong>My current output looks like the following:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;string xmlns="http://tempuri.org/"&gt;{ "Email": "james@example.com", "Active": true, "CreatedDate": "2013-01-20T00:00:00Z", "Roles": [ "User", "Admin" ] }&lt;/string&gt; </code></pre> <p><strong>This is my JQuery-Ajax call:</strong></p> <pre><code>$.ajax({ type: "GET", //dataType: "json", &lt;-- When I uncomment this line the request fails :( url: "http://webservices.domain.local/service.asmx/getData?name=", success: function(data){ console.log(data); // Output is: [object Document] instead of [object, object] }, error: function(XMLHttpRequest, textStatus, errorThrown){ } }); </code></pre> <p><strong>This is the code from my WebService:</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Runtime.Serialization; using System.Web; using System.Web.Script.Serialization; using System.Web.Script.Services; using System.Web.Services; using Newtonsoft.Json; namespace DataWebService { /// &lt;summary&gt; /// Summary description for Service1 /// &lt;/summary&gt; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class SPServices : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public string getData(string name) { Account account = new Account { Email = "james@example.com", Active = true, CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc), Roles = new List&lt;string&gt; { "User", "Admin" } }; return JsonConvert.SerializeObject(account, Formatting.Indented); } } public class Account { public string Email { get; set; } public bool Active { get; set; } public DateTime CreatedDate { get; set; } public IList&lt;string&gt; Roles { get; set; } } } </code></pre> <p><strong>The WebConfig File:</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;!--&lt;sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt; &lt;sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt; &lt;section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt; &lt;sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt; &lt;section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" /&gt; &lt;section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /&gt; &lt;section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /&gt; &lt;section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /&gt; &lt;/sectionGroup&gt; &lt;/sectionGroup&gt; &lt;/sectionGroup&gt;--&gt; &lt;/configSections&gt; &lt;appSettings/&gt; &lt;connectionStrings/&gt; &lt;system.web&gt; &lt;webServices&gt; &lt;protocols&gt; &lt;add name="HttpGet"/&gt; &lt;add name="HttpPost"/&gt; &lt;/protocols&gt; &lt;/webServices&gt; &lt;compilation debug="true" &gt; &lt;assemblies&gt; &lt;add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt; &lt;add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt; &lt;add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt; &lt;/assemblies&gt; &lt;/compilation&gt; &lt;!-- The &lt;authentication&gt; section enables configuration of the security authentication mode used by ASP.NET to identify an incoming user. --&gt; &lt;authentication mode="Windows" /&gt; &lt;!-- The &lt;customErrors&gt; section enables configuration of what to do if/when an unhandled error occurs during the execution of a request. Specifically, it enables developers to configure html error pages to be displayed in place of a error stack trace. &lt;customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"&gt; &lt;error statusCode="403" redirect="NoAccess.htm" /&gt; &lt;error statusCode="404" redirect="FileNotFound.htm" /&gt; &lt;/customErrors&gt; --&gt; &lt;pages&gt; &lt;controls&gt; &lt;add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;/controls&gt; &lt;/pages&gt; &lt;httpHandlers&gt; &lt;remove verb="*" path="*.asmx"/&gt; &lt;add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;!--&lt;add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/&gt;--&gt; &lt;/httpHandlers&gt; &lt;httpModules&gt; &lt;add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;/httpModules&gt; &lt;/system.web&gt; &lt;system.codedom&gt; &lt;compilers&gt; &lt;compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;providerOption name="CompilerVersion" value="v3.5"/&gt; &lt;providerOption name="WarnAsError" value="false"/&gt; &lt;/compiler&gt; &lt;/compilers&gt; &lt;/system.codedom&gt; &lt;!-- The system.webServer section is required for running ASP.NET AJAX under Internet Information Services 7.0. It is not necessary for previous version of IIS. --&gt; &lt;system.webServer&gt; &lt;validation validateIntegratedModeConfiguration="false"/&gt; &lt;modules&gt; &lt;remove name="ScriptModule" /&gt; &lt;add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;/modules&gt; &lt;handlers&gt; &lt;remove name="WebServiceHandlerFactory-Integrated"/&gt; &lt;remove name="ScriptHandlerFactory" /&gt; &lt;remove name="ScriptHandlerFactoryAppServices" /&gt; &lt;remove name="ScriptResource" /&gt; &lt;add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt; &lt;/handlers&gt; &lt;httpProtocol&gt; &lt;customHeaders&gt; &lt;add name="Access-Control-Allow-Origin" value="*" /&gt; &lt;add name="Access-Control-Allow-Headers" value="Content-Type" /&gt; &lt;/customHeaders&gt; &lt;/httpProtocol&gt; &lt;/system.webServer&gt; &lt;runtime&gt; &lt;assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1"&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/&gt; &lt;bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/&gt; &lt;/dependentAssembly&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/&gt; &lt;bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/&gt; &lt;/dependentAssembly&gt; &lt;/assemblyBinding&gt; &lt;/runtime&gt; &lt;/configuration&gt; </code></pre> <p>I would be really happy if someone could help me... I spent the whole weekend into this but I couldn´t find a solution for this... :-(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload