我有一个应该是相对简单的任务,坦率地说这让我很难过。我一直研究它,直到我的大脑被炸毁,现在我正在下注,并寻求你们的帮助。
这是场景:
WebService
我有一个用、WebServiceBinding
和ScriptService
属性 装饰的 ASPX 页面 (Q2.aspx) 。- 该页面包含一个方法 ,
GetAllContacts
用WebMethod
属性修饰并返回一个包含 JSON 数据的字符串。(对于它的价值,页面本身不包含其他控件或功能。) - 我有一个包含 JavaScript 的 HTML 页面,它使用该
XmlHttpRequest
对象调用GetAllContacts
ASPX 页面上的 WebMethod 并将 JSON 数据转换为 HTML 表。 - 我已验证我的
Web.Config
文件包含适当的协议处理程序,HttpGet
适用HttpPut
于.WebServices
System.Web.webServices
- 我已验证我的
Web.Config
文件包含该部分ScriptModule
下的条目System.webServer.modules
,并且它与相应的文档相匹配。
但是,当我在浏览器中查看 HTML 页面时,会发生以下情况:
- Web 请求通过,但结果是来自 ASPX 页面的未处理 HTML。
- 该
GetAllContacts
方法永远不会被调用,正如在其代码中设置断点所证明的那样。 - 然而,调用 Web 服务的代码被调用,并且在请求完成时调用的 JavaScript 回调函数被正确调用。
似乎 JavaScript 代码在很大程度上设置正确,但由于某种原因,我现在完全无法理解,HTML 页面根本不会WebMethod
在 ASPX 页面上执行,而是简单地返回页面,就好像它是一个普通的 HTMLGET
要求。显然,JavaScript 的函数无法评估 HTML 文档eval
,这让我想到了我的问题。(另请注意,JSON 数据不会出现在返回的 HTML 中。)
坦率地说,我很困惑。我看过几十篇 Microsoft 文章、StackOverflow 帖子、CodeProject 文章,还有谁知道。我的代码看起来没问题。但我更清楚。我错过了一些简单、愚蠢和明显的东西。我只需要有人向我指出。
您将在下面找到 ASPX 页面代码和 HTML 代码,希望它们能有所帮助。
ASPX 代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Q2.aspx.cs" Inherits="Satuit.Q2" enablesessionstate="False" %>
<html>
<body>
<form runat="server" id="frmMain"/>
</body>
</html>
-- Codebehind
using System.IO;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace Satuit
{
[WebService(Namespace="http://tempuri.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public partial class Q2 : Page
{
[WebMethod]
public static string GetAllContacts()
{
return LoadJsonData();
}
private static string LoadJsonData()
{
using (var stringWriter = new StringWriter())
{
string xmlUri = HttpContext.Current.Server.MapPath("\\XmlData\\Contacts.xml");
string xslUri = HttpContext.Current.Server.MapPath("\\XmlData\\Q2.xsl");
using (var xmlTextReader = new XmlTextReader(xmlUri))
{
var xpathDocument = new XPathDocument(xmlTextReader);
var xslTransform = new XslCompiledTransform();
xslTransform.Load(xslUri);
xslTransform.Transform(xpathDocument, null, stringWriter);
return stringWriter.ToString();
}
}
}
}
}
HTML 代码
var objectData; // Receives the objectified results of the JSON request.
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
xmlhttp.open("GET", "/Q2.aspx/GetAllContacts", true);
xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState == 4)
{
if (xmlhttp.status == 200)
{
var jsonResultBuffer = xmlhttp.responseText;
objectData = eval(jsonResultBuffer);
DisplayTable();
}
}
};
xmlhttp.send(null);
function DisplayTable()
{
var sHtml = "";
sHtml = "<table><tr><th>ID</th><th>First</th><th>Last</th><th>Address</th></tr>";
for(i = 0; i < objectData.length; i++)
{
sHtml += "<tr>";
sHtml += "<td>" + objectData.ID;
sHtml += "<td>" + objectData.firstName + "</td>";
sHtml += "<td>" + objectData.lastName + "</td>";
sHtml += "<td>" + objectData.address + "</td>";
sHtml += "</tr>"
}
sHtml += "</table>"
document.getElementById("divTable").innerHTML = sHtml;
}
</script>
开发环境详细信息
- 远景终极 SP 2
- 视觉工作室 2008
- .NET 框架 3.5
- 解决方案尚未部署,因此它在 Visual Studio 提供的“本地 Web 服务器”中运行。(让我想知道我是否不应该只在 Vista 下部署 IIS。)
- 请注意,包含 WebMethod 的 ASPX 页面和 HTML 页面位于同一解决方案中。