Ajax不会在服务器上命中C# [英] Ajax doesn't hit C# on server
问题描述
我必须使用 JQuery.Ajax()
自动完成某些字段.问题在于它在本地运行,但在服务器上无法运行一次.我写了一个测试页,在这里我只称Ajax(Test.aspx).
I have to use JQuery.Ajax()
to autocomplete some fields. The problem is that it works on local but not once on the server. I wrote a test page where I only call Ajax (Test.aspx).
Test.aspx中的JQuery:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "Test.aspx/getData",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{}', // same problem without that line
success: function (response) {
alert(response);
},
error: function (request, status, error) {
alert(error);
}
});
});
</script>
URL很好:当我仅指定 url:"Test.aspx"
时,它会触发Page_Load.当我指定 url:"Test.aspx/getData"
时,它没有命中该函数,而Ajax返回整个HTML页面(这意味着它无法到达该函数).我还测试了 url:"Test.aspx/getData/"
,但没有成功.
The url is good : when I only specify url : "Test.aspx"
it fires Page_Load.
When I specify url : "Test.aspx/getData"
it doesn't hit the function and Ajax returns the whole HTML page (meaning that it could not reach the function). I also tested url : "Test.aspx/getData/"
without success.
C#
[System.Web.Services.WebMethod]
public static string getData()
{
Dictionary<string, string> name = new Dictionary<string, string>();
name.Add("1", "Valeur 1");
name.Add("2", "Valeur 2");
string myJsonString = (new JavaScriptSerializer()).Serialize(name);
return myJsonString;
}
在这里,我想我已经指定了所有必需的内容: WebMethod
,它是静态属性,该函数返回一个JSON对象...
Here I think I have specified all the required stuff: WebMethod
, the static attribute, the function returns a JSON oject...
在我的web.config中,我在system.web中有这行:
In my web.config, I have this line in system.web :
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e3"/>
</httpModules>
我的应用程序托管在Windows Server 2008(IIS 6.1)上.
My application is hosted on a Windows Server 2008 (IIS 6.1).
我花了几个小时解决这个问题,但在网络上找不到任何解决方法.
I spent hours on that problem and didn't find anything on the web to resolve it.
在将< asp:ScriptManager ID ="ScriptManager1" runat ="server" EnablePageMethods ="true"/>
包含在其中之后,我还尝试了 PageMethods.getData
形式.结果仍然是HTML页面.
I also tried PageMethods.getData
after including <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
inside the form. The result is still the HTML page.
推荐答案
我对您的代码进行了一些更改.发表在这里的作品:
I made some changes to your code. Posted here works:
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function () {
$("#doAjax").click(function () {
$.ajax({
type: "POST",
url: "Test.asmx/getData",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{}', // same problem without that line
success: function (response) {
alert(response.d);
},
error: function (request, status, error) {
alert(error);
}
});
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" value="doAjax" id="doAjax" />
</form>
</body>
</html>
Web服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Test : System.Web.Services.WebService
{
[WebMethod]
public string getData()
{
Dictionary<string, string> name = new Dictionary<string, string>();
name.Add("1", "Valeur 1");
name.Add("2", "Valeur 2");
string myJsonString = (new JavaScriptSerializer()).Serialize(name);
return myJsonString; }
}
这篇关于Ajax不会在服务器上命中C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!