c# – jquery ajax与asp.net无法正常工作
我准备把剩下的头发拉出来,所以如果你知道问题可能会帮助我…谢谢.
我所有的谷歌搜索和搜索都没有得到回报. 首先,我使用的是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,errorThrown){ alert('errorThrown: ' + errorThrown); } }); 和aspx页面方法: [WebMethod] public static string GetTestJson() { return "Hello My World"; } 我也试过设置一个Web服务,但得到了相同的结果. 这是我没有运气的其他事情: [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) 任何想法或建议? 谢谢 解决方法
感谢@DaveWard和@CodeRoller的帮助,这导致了这个解决方案:
>安装ASP.NET Ajax扩展(HERE) 我的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,PublicKeyToken=31BF3856AD364E35"/> </assemblies> </compilation> <httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,System.Web.Extensions,PublicKeyToken=31bf3856ad364e35"/> </httpModules> </system.web> 我希望其他人会觉得这很有用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |