Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery - How to force datatype as JSON in ASP.Net 2.0

I got the following working after spinning my head around a thousand times and then referring parserrror SyntaxError: Unexpected token < - Load 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 get it to work, I am using dataType: "html". When I use JSON as datatype I get a parse error: Unexpected token <

How can we make it work with JSON?

Note: Though I am using IE8, some of my users are still using 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 Headers

enter image description here

References

  1. Differences between contentType and dataType in jQuery ajax function
  2. What is content-type and datatype in an AJAX request?
  3. How to return JSON from a 2.0 asmx web service
  4. ASP.NET AJAX PageMethods call load whole page for .NET 4.5 IIS 7.5
  5. Support cross-domain requests (specifically multiple methods in WebInvoke) in Rest WCF
  6. jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox
  7. Cannot set content-type to 'application/json' in jQuery.ajax
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

then in response header you should see

        content-type:application/json 

not content-type:text/html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...