使用XmlHttpRequest和纯JavaScript调用WebMethods
我有一个相对简单的任务,坦率地说,这让我感到困惑。我一直在研究它,直到我的大脑被炸了,现在我在划船,并请求你们的帮助 以下是场景:使用XmlHttpRequest和纯JavaScript调用WebMethods,javascript,asp.net,ajax,xmlhttprequest,webmethod,Javascript,Asp.net,Ajax,Xmlhttprequest,Webmethod,我有一个相对简单的任务,坦率地说,这让我感到困惑。我一直在研究它,直到我的大脑被炸了,现在我在划船,并请求你们的帮助 以下是场景: 我有一个ASPX页面(Q2.ASPX),上面装饰着WebService, WebServiceBinding,以及ScriptService属性 该页面包含一个方法,GetAllContacts,该方法用WebMethod 属性,并返回包含JSON数据的字符串。(值得一提的是,这一页 本身不包含其他控件或功能。) 我有一个包含JavaScript的HTML页面,它
- 我有一个ASPX页面(Q2.ASPX),上面装饰着
,WebService
,以及WebServiceBinding
属性李>ScriptService
- 该页面包含一个方法,
,该方法用GetAllContacts
属性,并返回包含JSON数据的字符串。(值得一提的是,这一页 本身不包含其他控件或功能。)WebMethod
- 我有一个包含JavaScript的HTML页面,它使用
对象调用ASPX页面上的XmlHttpRequest
WebMethod并进行转换 将JSON数据转换为HTML表李>GetAllContacts
- 我已验证我的
文件是否包含适当的协议处理程序 对于Web.Config
下的System.Web.WebServices
部分中的WebServices
和HttpGet
HttpPut
- 我已验证我的
文件是否包含Web.Config
节,并且它与适当的文档匹配System.webServer.modules
- web请求已通过,但结果是来自ASPX页面的未处理HTML
- 从不调用
方法,在其代码中设置断点就是明证GetAllContacts
- 然而,调用Web服务的代码被调用,JavaScript回调也被调用 正确调用请求完成时调用的函数李>
WebMethod
,而是简单地返回页面,就像它是一个普通的HTMLGET
请求一样。显然,HTML文档不能通过JavaScript的eval
函数进行评估,这就引出了我的问题。(还请注意,JSON数据在返回的HTML中不会出现。)
坦率地说,我感到困惑。我看过几十篇微软的文章,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();
}
}
}
}
}
--代码隐藏
使用System.IO;
使用System.Web;
使用System.Web.Script.Services;
使用System.Web.Services;
使用System.Web.UI;
使用System.Xml;
使用System.Xml.XPath;
使用System.Xml.Xsl;
名称空间Satuit
{
[WebService(命名空间=”http://tempuri.org")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[脚本服务]
公共部分类Q2:第页
{
[网络方法]
公共静态字符串GetAllContacts()
{
返回LoadJsonData();
}
私有静态字符串LoadJsonData()
{
使用(var stringWriter=new stringWriter())
{
字符串xmlUri=HttpContext.Current.Server.MapPath(“\\XmlData\\Contacts.xml”);
字符串xslUri=HttpContext.Current.Server.MapPath(“\\XmlData\\Q2.xsl”);
使用(var xmlTextReader=newxmltextreader(xmlUri))
{
var xpathDocument=新xpathDocument(xmlTextReader);
var xslTransform=新的xslcomiledtransform();
xslTransform.Load(xslUri);
Transform(xpathDocument,null,stringWriter);
返回stringWriter.ToString();
}
}
}
}
}
HTML代码
var objectData;//接收JSON请求的对象化结果。
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}else if(window.ActiveXObject){
xmlhttp=新的ActiveXObject(“Msxml2.xmlhttp”);
}
open(“GET”,“/Q2.aspx/GetAllContacts”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
var jsonResultBuffer=xmlhttp.responseText;
objectData=eval(jsonResultBuffer);
DisplayTable();
}
}
};
xmlhttp.send(空);
函数DisplayTable()
{
var sHtml=“”;
sHtml=“IDFirstLastAddress”;
对于(i=0;i
开发环境详细信息
- Vista Ultimate SP 2
- Visual Studio 2008
- .NET Framework 3.5
- 解决方案尚未部署,因此正在“本地Web服务器”中运行 由VisualStudio提供。(让我想知道我是否不应该只部署IIS 在Vista下。)
- 请注意,包含WebMethod和HTML页面的ASPX页面位于 同样的解决方案
$.ajax({
type: "POST",
url: "Q2.aspx/GetAllContacts",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
alert("success");
},
error: function(response, aa) {
alert("fail");
}
});
星期四
$.ajax({
type: "POST",
url: "Q2.aspx/GetAllContacts",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
alert("success");
},
error: function(response, aa) {
alert("fail");
}
});
xmlhttp.open("POST", "/Q2.aspx/GetAllContacts", true);
xmlhttp.setRequestHeader("content-type", "application/json");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var jsonResultBuffer = JSON.parse(xmlhttp.responseText);
objectData = jsonResultBuffer.d;
DisplayTable();
}
};