Why is a Delphi 2007 ASP.NET AJAX call returning [object Object]
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 开发者_如何学JAVAwould 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.
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:
- Make sure you've got FireFox and FireBug installed;
- Press F12 to show the FireBug pane;
- Load your web page
- Go to the firebug
script
tab; - Add a breakpoint at the line where you're calling
alert
, by clicking in the gutter area; - Refresh the page;
- Inspect the
result
variable to see what you've got.
精彩评论