jQuery的阿贾克斯与asp.net不工作工作、阿贾克斯、jQuery、net

2023-09-10 13:29:33 作者:街混

我要拉出所有剩余的头发,我,所以请帮助我,如果你知道这个问题可能是...谢谢。 我所有的谷歌搜索和搜索没有任何回报。

I'm about to pull out any remaining hair that I have, so please help me out if you know what the problem might be... Thanks. All my googling and searching has not paid off either.

首先,我使用jQuery,1.7.2.min.js和ASP.net 2.0的Web表单。

First, I'm using jquery-1.7.2.min.js and ASP.net 2.0 web form.

我试图使用jQuery做一个Ajax调用,但不断收到语法错误/解析错误消息。我尝试了很多不同的方法,但它们都导致了错误,当我设置数据类型设置为JSON。

I'm trying to make an ajax call using jquery but keep getting syntax error/parse error messages. I've tried many different ways but they all result in the error when I set the dataType to 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页面的方法:

and the aspx page method:

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

我试图建立一个Web服务过,但得到了同样的结果。 看来,响应回来或者是完整的HTML页面或XML的Web服务,因为我设置的数据类型设置为JSON它不是成功解析它。 如果是这样的话,我该如何设置响应仅返回JSON?

I've tried setting up a web service too, but get the same result. It appears that the response coming back is either the full html for the page or xml from the web service and because I set the dataType to json it is not successful in parsing it. If this is the case, how do I set the response to only return json?

下面是别的东西,我已经试过没有运气:

Here is something else I've tried without luck:

[WebMethod]
public static string GetTestJson()
{
    HttpContext.Current.Response.ContentType = "application/json";
    string json = JavaScriptConvert.SerializeObject("Hello My World");
    return json;
}

错误信息我通过jQuery的获得:

Error messages I get through 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)

任何意见或建议?

Any ideas or suggestions?

感谢

推荐答案

感谢您@DaveWard和@ codeRoller您的帮助而导致该解决方案:

Thank you @DaveWard and @CodeRoller for your help which led to this solution:

在安装ASP.NET AJAX扩展(这里) 添加引用System.Web.Extensions.dll 修改web.config中包括ScriptModule HTTP模块(见下文)

我jQuery的AJAX调用和WebMethod的仍然是一样的我原来的职位。

My jQuery ajax call and the WebMethod are still the same as my original post.

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

My web.config has been modified as below:

  <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>

我希望其他人会发现这很有用。

I hope someone else will find this useful.