jquery – 如何在ASP.Net 2.0中强制数据类型为JSON

前端之家收集整理的这篇文章主要介绍了jquery – 如何在ASP.Net 2.0中强制数据类型为JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
旋转了我的头一千次,然后参考 parserrror SyntaxError: Unexpected token < – Load Partial View using jQuery Ajax in ASP.NET MVC 4,我得到了以下工作

我在ASP.Net 2.0项目中有以下代码.它工作 – 但要使它工作,我使用dataType:“html”.当我使用JSON作为数据类型时,我得到一个解析错误:意外的令牌< 我们如何使它与JSON一起使用? 注意:虽然我使用的是IE8,但我的一些用户仍在使用IE6.所以我需要一个适用于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

请求和响应标头

参考

> Differences between contentType and dataType in jQuery ajax function
> What is content-type and datatype in an AJAX request?
> How to return JSON from a 2.0 asmx web service
> ASP.NET AJAX PageMethods call load whole page for .NET 4.5 IIS 7.5
> Support cross-domain requests (specifically multiple methods in WebInvoke) in Rest WCF
> jQuery $.ajax(),$.post sending “OPTIONS” as REQUEST_METHOD in Firefox
> Cannot set content-type to ‘application/json’ in jQuery.ajax

解决方法

在请求中你需要设置Accept:application / json,如果你的服务器有json支持,那么它会在json中自动发送响应,
type: "GET",Accept: application/json,

然后在响应标题中你应该看到

content-type:application/json

内容类型:text / html的

原文链接:https://www.f2er.com/jquery/180528.html

猜你在找的jQuery相关文章