如何强制数据类型为JSON在ASP.Net 2.0 [英] How to force datatype as JSON in ASP.Net 2.0

查看:124
本文介绍了如何强制数据类型为JSON在ASP.Net 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下工作纺纱我的头周围的一千倍,然后参照<后href=\"http://stackoverflow.com/questions/18882705/parserrror-syntaxerror-unexpected-token-load-partial-view-using-jquery-ajax\">parserrror语法错误:意外的令牌LT; - 在ASP.NET中使用jQuery的Ajax MVC 4 负载管窥

我在 ASP.Net 2.0 项目如下code。它的工作原理 - 但要得到它的工作,我使用数据类型:HTML。当我使用JSON作为数据类型我得到一个解析错误:意外令牌LT;

我们

如何能使其与 JSON

工作

请注意:虽然我使用IE8,我的一些用户仍在使用IE6。所以,我需要在工作的解决方案的 IE6

的jQuery的Ajax

  $阿贾克斯({
            键入:GET,
            网址:admPlantParametersViewEdit.aspx /调用getResult
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:HTML,
            成功:函数(MSG)
            {
                警报(你好);
            },
            错误:errorFunction
        });

VB.Net

 &LT;的WebMethod()GT; _
    公共共享功能调用getResult()作为字符串        返回你好    结束功能

请求和响应头

参考


  1. Differences bettween的contentType和jQuery的AJAX功能的dataType

  2. What在AJAX请求的内容类型和数据类型?

  3. 如何从2.0返回JSON ASMX Web服务

  4. ASP.NET AJAX PageMethods呼叫负载整个页面的.NET 4.5 IIS 7.5

  5. 支持跨域请求(特别是多发于WebInvoke方法)在休息WCF

  6. jQuery $阿贾克斯(),$。员额发送&QUOT;方式];作为REQUEST_METHOD在Firefox

  7. Cannot设置jQuery.ajax
  8. 内容类型为应用程序/ JSON

解决方案

我想它指以下两个职位后

<一个href=\"http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3a%20Encosia%20%28Encosia%29\"相对=nofollow> encosia - ASP.NET Web服务的错误:手动JSON序列

encosia - ASMX ScriptService错误:安装和配置


  

由于您使用的.NET 2.0中,最有可能的罪魁祸首是你已经安装了AJAX的扩展,但还没有更新你的web.config使用该新的处理程序ASMX请求。


的措施来解决:


  1. 下载ASP.NET AJAX 1.0和安装它,如果尚未
    http://www.microsoft.com/en-us/download /details.aspx?id=883


  2. 添加适当的HttpHandler如下图所示拆除现有一个接[此配置对 NET 2.0 只。请参阅其他版本上面提到的博客]

     &LT;&HttpHandlers的GT;    &LT;清除动词=*路径=* ASMX。/&GT;    &LT;添加动词=*路径=*。ASMX验证=假
             类型=System.Web.Script.Services.ScriptHandlerFactory,
             System.Web.Extensions程序,版本= 1.0.61025.0,
                         文化=中性公钥= 31bf3856ad364e35/&GT;&LT; / HttpHandlers的&GT;


  3. Ajax和asp.net不工作


 &LT;&的HttpModules GT;
    &LT;添加名称=ScriptModuleTYPE =System.Web.Handlers.ScriptModule,System.Web.Extensions程序,版本= 1.0.61025.0,文化=中性公钥= 31bf3856ad364e35/&GT;
  &LT; /&的HttpModules GT;

 &LT;&集会GT;
            &LT;添加组件=System.Web.Extensions程序,版本= 1.0.61025.0,文化=中性公钥= 31BF3856AD364E35/&GT;
            &LT;添加组件=System.Web.Extensions.Design,版本= 1.0.61025.0,文化=中性公钥= 31BF3856AD364E35/&GT;
        &LT; /组件&GT;

0.4。使得它为 POST JSON

  $阿贾克斯({
            键入:POST,
            网址:admPlantParametersViewEdit.aspx /调用getResult
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON
            成功:函数(MSG)
            {
                警报(msg.d);
                警报(MSG);
            },
            错误:errorFunction
        });


这个答案将是不完整引述从encosia以下行


  

返回原始JSON的能力ASMX服务由最初由ASP.NET AJAX 1.0的扩展增加了两个关键功能成为可能。


  
  

      
  1. 的JavaScriptSerializer

  2.   
  3. ScriptHandlerFactory

  4.   

好读


  1. 斯科特谷的 - JSON劫持和如何ASP.NET AJAX 1.0避免这些攻击

  2. 一个破ASP.NET AJAX版本<之间的变化/ A>

  3. 阿贾克斯jQuery的asp.net错误意外的令牌LT;

  4. ASMX ScriptService错误:安装和配置

  5. 使用的ASP.NET Web API使用JSON.NET

  6. jQuery的,ASP的.NET Web API,并Json.NET走进酒吧......

  7. 部署问题ASP.NET AJAX应用程序

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

References

  1. Differences bettween 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

解决方案

I figured it out after referring following two posts

encosia - ASP.NET web services mistake: manual JSON serialization

encosia - ASMX ScriptService mistake: Installation and configuration

Since you’re using .NET 2.0, the most likely culprit is that you’ve got the AJAX Extensions installed, but haven’t updated your web.config to use that new handler for ASMX requests.

STEPS TO SOLVE:

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

  2. Add proper httpHandler after removing the existing one as shown below [This configuration is for .Net 2.0 only. Refer 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>
    

  3. Refer 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>

.4. Made it as 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 won’t 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 the ASP.NET AJAX Extensions v1.0

  1. JavaScriptSerializer
  2. ScriptHandlerFactory

Good Reads

  1. Scott Gu's - JSON Hijacking and How ASP.NET AJAX 1.0 Avoids these Attacks
  2. A breaking change between versions of ASP.NET AJAX
  3. ajax jQuery asp.net error Unexpected token <
  4. ASMX ScriptService mistake: Installation and configuration
  5. Using JSON.NET with ASP.NET Web API
  6. jQuery, ASP.NET Web API, and Json.NET walk into a bar…
  7. Deployment problems with ASP.NET AJAX applications

这篇关于如何强制数据类型为JSON在ASP.Net 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆