How to force data type as JSON in ASP.Net 2.0

I got the following to work after I turned my head about a thousand times and then referred to parserrror SyntaxError: Unexpected token <- Loading partial view using jQuery Ajax in ASP.NET MVC 4

I have the following code in my ASP.Net 2.0 project. It works, but to make it work, I use dataType: "html" . When I use JSON as the data type, I get a parsing error: Unexpected token <

How can we work with JSON ?

Note. Although I use IE8, some of my users still use IE6. So I need a solution that works in IE6 .

jQuery Ajax

  $.ajax({ type: "GET", url: "admPlantParametersViewEdit.aspx/GetResult", contentType: "application/json; charset=utf-8", dataType: "html", success: function(msg) { alert("Hi"); }, error: errorFunction }); 

Vb.net

  <WebMethod()> _ Public Shared Function GetResult() As String Return "hello" End Function 

Request and Response Header

enter image description here

References

  • Differences between contentType and dataType in jQuery ajax function
  • What is the content type and data type in an AJAX request?
  • How to return JSON from asmx web service 2.0
  • ASP.NET AJAX PageMethods Causes Full Page Load for .NET 4.5 IIS 7.5
  • Support for cross-domain queries (in particular, several methods in WebInvoke) in Rest WCF
  • jQuery $ .ajax (), $ .post sending "OPTIONS" as REQUEST_METHOD in Firefox
  • Unable to set content type to 'application / json' in jQuery.ajax
+1
source share
2 answers

I realized this after linking to the following posts

encosia - ASP.NET web services error: manually serializing JSON

encosia - ASMX ScriptService error: installation and configuration

Since you are using .NET 2.0, the most likely culprit is that you have AJAX Extensions installed, but havent updated your web.config to use this new handler for ASMX requests.

STEPS FOR SOLUTION:

  • Download ASP.NET AJAX 1.0 and install it if it has not already been done http://www.microsoft.com/en-us/download/details.aspx?id=883

  • Add the correct httpHandler after deleting the existing one, as shown below. [This configuration is for .Net 2.0 only . Refer to the blog mentioned above for other versions]

      <httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </httpHandlers> 
  • Contact jquery ajax with asp.net not working

As

  <httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </httpModules> 

and

  <assemblies> <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </assemblies> 

0.4. Made it like POST and json

  $.ajax({ type: "POST", url: "admPlantParametersViewEdit.aspx/GetResult", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); alert(msg); }, error: errorFunction }); 

This answer would not be complete without quoting the following lines from encosia

The ability for ASMX services to return raw JSON is made possible by two key features originally added by ASP.NET AJAX Extensions v1.0

  • JavaScriptSerializer
  • ScriptHandlerFactory

Good readings

0
source share

in the request you need to install Accept: application / json, if you have json support then it will automatically send the response to json,

  type: "GET", url: "admPlantParametersViewEdit.aspx/GetResult", contentType: "application/json; charset=utf-8", Accept: application/json, 

then in the response header you should see

  content-type:application/json 

not content type: text / html

0
source share

All Articles