代码之家  ›  专栏  ›  技术社区  ›  Kelly Robins

ASP.net JSON Web服务响应类型存在问题

  •  3
  • Kelly Robins  · 技术社区  · 17 年前

    我正在尝试编写一个ASP.NETWeb服务,jQueryAjax调用将使用该服务。

    当我尝试进行ajax调用(通过jquery)时,我从服务器获得了一个成功的响应,但由于解析器错误,请求失败。

    我已经验证了webservice返回的json,它是有效的。这个问题似乎源于asp.net将json对象作为xml返回的事实。

    我已经使用

    <Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
    

    我添加了以下http处理程序,因为它被认为是一个潜在的修复程序

    <httpHandlers>
        <remove verb="*" path="*.asmx" />
        <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false" />
    </httpHandlers>
    

    我可以通过删除数据类型成功地发出请求,但我非常希望避免使用eval来反序列化数据。


    JAVASCRIPT

    (function($) {
      $.ajaxSetup({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        global: false,
        dataType: "json"
      });
    
      function auto() {
        console.log("test");
        return;
      };
    
      $.fn.species = {
        test: function() {
          alert("test");
        },
        load: function() { //load and attach species list to element as dropdown
          $.ajax({
            url: "SpeciesService.asmx/List",
            success: function(msg) {
              console.log(msg);
            },
            error: function(xhr, desc, exceptionobj) {
              console.log(xhr.responseText);
              console.log(desc);
              console.log(exceptionobj);
            }
          });
          return this;
        }
      }; //Species Block
    })(jQuery); //jQuery Alias Block
    

    ASP.NET Web服务

    <%@ WebService Language="VB" Class="SpeciesService" %>
    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports Species
    Imports System.Runtime.Serialization
    

    '要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。 ' _

    _ 公共类物种服务

    Inherits System.Web.Services.WebService
    <WebMethod()> _
    <Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
    Public Function Data(ByVal id As Integer) As String
        Dim curSpecies As New Species(id)
        Return curSpecies.serialize
    End Function
    
    <WebMethod()> _
    <Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
    Public Function List() As String
    
        Return Species.list()
    End Function
    

    末级

    4 回复  |  直到 6 年前
        1
  •  3
  •   Çağdaş Tekin    17 年前

    尝试使用JQuery发布伪json数据,如下所示:

    $.ajaxSetup({
        type: "POST",
        data : "{'foo':'bar'}"
    ...
    
        2
  •  1
  •   Rashack    17 年前

    这与其说是一个答案,不如说是一个提示——我一直在使用PageMethods,而不是webservices,而且效果非常好。如果它遇到同样的问题,我会试试。

        3
  •  1
  •   JeremyWeir    17 年前

    我认为它使用设置为json的数据类型的原因是jquery在这种情况下为您添加了一个空的帖子正文。

        4
  •  0
  •   Kelly Robins    17 年前

    好的,非常感谢你们的帮助。

    我甚至不知道是什么修复了问题的服务器错误部分。我只是花了十分钟心不在焉地处理语法(修正缩进、大写、注释),因为我太沮丧了,无法专注于手头的问题。我刷新了页面,突然它工作正常。所以答案是发布虚拟数据并修复随机的神秘语法错误。

    不管怎样,我还是非常感谢你让我回到正确的轨道上。