jQuery与ASP.NET的AJAX无法正常工作

6
我即将抓狂,如果你知道问题所在,请帮帮我...谢谢。 我已经搜索了很多,但都没有找到答案。
首先,我正在使用jquery-1.7.2.min.js和ASP.net 2.0 web表单。
我正在尝试使用jquery进行ajax调用,但一直收到语法错误/解析错误的消息。我尝试了很多不同的方式,但当我将dataType设置为json时,它们都会导致错误。
这是我的代码:
$.ajax({
    type: "POST",
    url: "UserList.aspx/GetTestJson",
    data: {}, //have also tried "{}" and other options such as removing the line
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function(data, textStatus, jqXHR){
        alert('success');
    },
    //Using below instead of above makes no difference either
    //success: function(data){
    //  alert('success');
    //},
    error: function(jqXHR, textStatus, errorThrown){
        alert('errorThrown: ' + errorThrown);
    }
});

以及 aspx 页面方法:

[WebMethod]
public static string GetTestJson()
{
    return "Hello My World";
}

我尝试搭建一个web服务,但是结果相同。看起来返回的响应要么是完整的页面HTML,要么是来自web服务的XML,并且由于我将dataType设置为JSON,所以无法成功解析它。 如果是这种情况,我该如何只设置响应返回JSON? 这是我尝试了另外一种方法,但没有成功:
[WebMethod]
public static string GetTestJson()
{
    HttpContext.Current.Response.ContentType = "application/json";
    string json = JavaScriptConvert.SerializeObject("Hello My World");
    return json;
}

我通过jQuery收到的错误信息:

errorThrown.message = "Syntax error"
errorThrown.number = -2146827286
errorThrown.name = "SyntaxError"
textStatus = "parseerror"
jqXHR.status = 200
jqXHR.statusText = "OK"
jqXHR.readyState = 4
jqXHR.responseText = (the complete html of the page)

有任何想法或建议吗?

谢谢


仅仅设置Content-type HTTP头并不能神奇地改变响应内容。你需要自己将数据转换为JSON格式。此外,[WebMethod]通常用于SOAP服务,而不是RESTful风格的Web服务。 - Dai
1
这部分是正确的,PageMethods专门用于AJAX场景,它们默认将响应类型设置为JSON。 - Jupaol
1
你正在使用哪个版本的ASP.NET? - Dave Ward
2
@Rob:听起来你没有安装ASP.NET Ajax扩展或你的web.config没有更新以利用那些新的特性(包括你正在尝试使用的静态“Page Method”功能)。我在这里写了一些关于如何设置的内容:http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/。 - Dave Ward
2
嗨@Rob。试一下我刚写的这个例子。正如Dave所提到的,测试代码需要正确配置web.config,请在此处检查代码(使用可工作的web.config):http://www.mediafire.com/?5l7va36edkc3tkl - CoderRoller
显示剩余5条评论
2个回答

12

感谢@DaveWard和@CodeRoller的帮助,他们提供了这个解决方案:

  1. 安装ASP.NET Ajax扩展(这里
  2. 添加对System.Web.Extensions.dll的引用
  3. 修改web.config以包含ScriptModule httpModule(见下文)

我的jQuery ajax调用和WebMethod与我最初的帖子相同。

我的web.config已被修改如下:

  <system.web>
    <compilation debug="true">
      <assemblies>
        <!-- A bunch of other assemblies here-->
        <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>
    </compilation>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </httpModules>
  </system.web>

希望这个对其他人有用。


你的帖子帮助我向我的ASP.NET 2.0 Web应用程序发送JSON!谢谢! - Roman
@PostureOfLearning 如果我能给你一千个赞就好了!! - Suraj Singh
3
非常欢迎。我很高兴这篇文章能帮到其他人。我也曾因为这个问题而焦头烂额... =) - PostureOfLearning
非常感谢!我已经撞了几个小时的墙了... - Vítor Martins

-2

你应该在 PageLoad 函数 中调用一个函数。

If(!Page.IsPostback){
   ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接