为什么 Delphi 2007 ASP.NET AJAX 调用返回 [object Object]
有人成功使用 Delphi 2007 中支持 AJAX 的 ASP.NET Web 应用程序向导来进行 ajax 调用吗?如果是这样,让它发挥作用的秘诀是什么?
我问有两个原因。首先,我所尝试的方法似乎不起作用。其次,我广泛搜索了 Web,但没有看到任何示例或讨论表明 AJAX 实际上可以在 Delphi 2007 中与 ASP.NET 2.0 一起使用。
这是发生的事情。我使用 .NET 4.0 构建了一个支持 AJAX 的 C# 应用程序(以及相应的 C# Web 服务)。一切都非常简单,而且也按应有的方式运行。
我想从 Delphi 2007 ASP.NET 应用程序中调用这些相同的 WebMethods。不幸的是,当 C# 客户端从 Web 服务接收各种 WebMethods 返回的字符串时,Delphi 2007 客户端接收 [object Object],并且它似乎有两个未定义的属性。我这样说是因为以下代码在用于接收结果时,会显示一个警报窗口,其中显示“未定义未定义”。
function ShowObjectInfo(result) {
var Name;
var str;
for (Name in result) {
if (typeof result[name] !== 'function') {
str = str + ' ' + result[name]
}
}
window.alert(str);
}
这是我可以整理的最简单的例子。这是我的 ASPX 文件:
<%@ Page language="c#" Debug="true" Codebehind="Default.pas" AutoEventWireup="false" Inherits="Default.TWebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head runat="server">
<title>Keeping it simple</title>
<script type="text/javascript">
function OnLookup()
{
WebService.Echo('repeat after me', OnLookupComplete, OnError);
}
function OnLookupComplete(result)
{
window.alert('Success :' + result);
}
function OnError(result)
{
window.alert('Error ' + result);
}
</script>
</head>
<body>
<form runat="server">
<asp:ScriptManager id="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference path="../../statisticsservice/statisticsservice.asmx"></asp:ServiceReference>
</Services>
</asp:ScriptManager>
<asp:UpdatePanel id="UpdatePanel1" runat="server"></asp:UpdatePanel>
<input value="Button" type="button" onclick="OnLookup();">
</form>
</body>
</html>
很明显,Web 服务实际上正在执行,因为对进程密集型 WebMethod 的调用需要更长的时间才能返回。尽管如此,我从 Delphi 应用程序执行后看到的是一个警报窗口,其中包含“成功:[object Object]”
因此,重复一下这个问题,是否有人成功使用 Delphi 2007 中支持 AJAX 的 ASP.NET Web 应用程序向导来进行ajax调用?如果是这样,让它发挥作用的秘诀是什么?
检查回调函数中返回的值后(基于 Wouter 建议的 FireFox 和 FireBug 的使用),以下是简单示例中 JavaScript 的修复。
function OnLookup()
{
WebService.Echo('repeat after me', OnLookupComplete, OnError);
}
function OnLookupComplete(result)
{
window.alert('Success :' + result.d);
}
function OnError(result){
window.alert('Error ' + result._message);
}
</script>
一些注释。首先,我仍然不知道为什么在名为 d 的属性中找到返回的字符串,但至少它有效。其次,最好将 JavaScript 函数和变量声明放在与 HTML 分开的文件中。执行此操作时,您将引用外部 JavaScript 文件或 ScriptManager 的 Scripts 属性中的文件。为了简单起见,我在此处的 HTML 文件中包含了 JavaScript。
Has anyone successfully used the AJAX-enabled ASP.NET Web Application wizard in Delphi 2007 to make ajax calls? If so, what's the secret to making it work.
I ask for two reasons. First, what I have tried does not seem to work. Second, I have searched the Web extensively and have seen no examples or discussions that imply that the AJAX actually worked in Delphi 2007 with ASP.NET 2.0.
Here's what's going on. I built an AJAX enabled C# application (and a corresponding C# Web service) using .NET 4.0. It was all pretty straightforward, and it is working as it should be.
I would like to call those same WebMethods from a Delphi 2007 ASP.NET application. Unfortunately, while the C# client receives the strings returned from the various WebMethods from the Web service, the Delphi 2007 client receives [object Object], and it appears to have two undefined properties. I say this because the following code, when used to receive the result, displays an alert window displaying "undefined undefined."
function ShowObjectInfo(result) {
var Name;
var str;
for (Name in result) {
if (typeof result[name] !== 'function') {
str = str + ' ' + result[name]
}
}
window.alert(str);
}
Here is the most simple example I could put together. Here is my ASPX file:
<%@ Page language="c#" Debug="true" Codebehind="Default.pas" AutoEventWireup="false" Inherits="Default.TWebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head runat="server">
<title>Keeping it simple</title>
<script type="text/javascript">
function OnLookup()
{
WebService.Echo('repeat after me', OnLookupComplete, OnError);
}
function OnLookupComplete(result)
{
window.alert('Success :' + result);
}
function OnError(result)
{
window.alert('Error ' + result);
}
</script>
</head>
<body>
<form runat="server">
<asp:ScriptManager id="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference path="../../statisticsservice/statisticsservice.asmx"></asp:ServiceReference>
</Services>
</asp:ScriptManager>
<asp:UpdatePanel id="UpdatePanel1" runat="server"></asp:UpdatePanel>
<input value="Button" type="button" onclick="OnLookup();">
</form>
</body>
</html>
It is clear that the Web service is actually executing, since calls to more process intensive WebMethods takes longer to return. Nonetheless, what I see after execution from the Delphi application is an alert window that contains "Success : [object Object]"
So, to repeat the question, has anyone successfully used the AJAX-enabled ASP.NET Web Application wizard in Delphi 2007 to make ajax calls? If so, what's the secret to making it work.
After inspecting the value returned in the callback functions (based on the use of FireFox and FireBug as Wouter suggested), the following is the fix to the JavaScript in the simple example.
function OnLookup()
{
WebService.Echo('repeat after me', OnLookupComplete, OnError);
}
function OnLookupComplete(result)
{
window.alert('Success :' + result.d);
}
function OnError(result){
window.alert('Error ' + result._message);
}
</script>
A couple of notes. First, I still do not know why the returned string is found in a property named d, but at least it works. Second, it is better if you place your JavaScript functions and variable declarations in a file separate from your HTML. When you do that, you refer to the external JavaScript file or files in the Scripts property of the ScriptManager. I included the JavaScript in the HTML file here for simplicity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还没有人回答这个问题,所以让我至少尝试以一种或另一种方式提供帮助。
上面的
ShowObjectInfo
函数让我认为您目前没有使用 FireBug 。为了加快调试过程,你可以这样做:
script
选项卡;alert
的行添加断点;result
变量以查看您得到的结果。Nobody's answered this one yet, so let me at least try to help out in one way or the other.
The
ShowObjectInfo
function above makes me think that you're not using FireBug at the moment.To speed up the debugging-process, you could do this:
script
tab;alert
, by clicking in the gutter area;result
variable to see what you've got.