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.
我要把我剩下的头发拔掉,所以如果你知道问题出在哪里,请帮助我。谢谢。我所有的搜索和搜索也没有得到回报。
First, I'm using jquery-1.7.2.min.js and ASP.net 2.0 web form.
首先,我使用jquery-1.7.2.min。js和ASP.net 2.0 web表单。
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.
我正在尝试使用jquery进行ajax调用,但是不断地获得语法错误/解析错误消息。我尝试了许多不同的方法,但当我将数据类型设置为json时,它们都会导致错误。
Here's what I have:
这就是我有:
$.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);
}
});
and the aspx page method:
以及aspx页面方法:
[WebMethod]
public static string GetTestJson()
{
return "Hello My World";
}
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?
我也尝试过设置web服务,但是得到了相同的结果。返回的响应要么是页面的完整html,要么是来自web服务的xml,因为我将数据类型设置为json,所以解析它并不成功。如果是这种情况,如何将响应设置为仅返回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;
}
Error messages I get through jQuery:
通过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?
有什么想法和建议吗?
Thanks
谢谢
1 个解决方案
#1
12
Thank you @DaveWard and @CodeRoller for your help which led to this solution:
感谢@DaveWard和@CodeRoller的帮助,使我们有了这个解决方案:
- Install ASP.NET Ajax Extensions (HERE)
- 安装ASP。净Ajax扩展(在这里)
- Add reference to System.Web.Extensions.dll
- 添加引用System.Web.Extensions.dll
- Modify web.config to include the ScriptModule httpModule (see below)
- 修改web。配置以包含ScriptModule httpModule(参见下面)
My jQuery ajax call and the WebMethod are still the same as my original post.
我的jQuery ajax调用和WebMethod仍然与我最初的文章相同。
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.
我希望其他人会发现这很有用。
#1
12
Thank you @DaveWard and @CodeRoller for your help which led to this solution:
感谢@DaveWard和@CodeRoller的帮助,使我们有了这个解决方案:
- Install ASP.NET Ajax Extensions (HERE)
- 安装ASP。净Ajax扩展(在这里)
- Add reference to System.Web.Extensions.dll
- 添加引用System.Web.Extensions.dll
- Modify web.config to include the ScriptModule httpModule (see below)
- 修改web。配置以包含ScriptModule httpModule(参见下面)
My jQuery ajax call and the WebMethod are still the same as my original post.
我的jQuery ajax调用和WebMethod仍然与我最初的文章相同。
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.
我希望其他人会发现这很有用。