【问题标题】:AJAX enabled WCF Service function to redirect to a new windowAJAX 启用 WCF 服务功能以重定向到新窗口
【发布时间】:2011-04-01 03:19:55
【问题描述】:

场景:我将使用 AJAX 访问外部 Web 服务 [ExternalWS]。所以很明显,需要先创建本地代理服务[LocalProxyWS],然后再访问外部的web服务。现在,外部服务 web 方法 [Process] 基本上将我们网站上的当前页面重定向到他们的网站,做一些工作然后返回到我们的网站。

我想要什么:我希望当用户点击我们网站上的按钮(“进程”)时,它应该打开一个新窗口,然后开始在新窗口上执行请求,所以我可以让我的网站上的页面永久显示(它将每 15 秒向外部服务(通过本地代理)轮询一次请求以获取状态)。

希望我的解释清楚。

以下是我目前的代码,如果有其他有效的替代方法,请随时发表评论..

调用外部服务的本地代理服务如下..

    [ServiceContract(Namespace = "LocalProxy")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class LocalProxyToExternalService
{
    [OperationContract]
    public void InitiateTransaction(string amount)
    {
        //NOTE: Basically want to call the external service from here in 
                a new window.....
        //HttpContext.Current.Response.Redirect("www.google.com", false);
    }
}

我的页面代码如下...

        function PayWithXYZ() {

         var newWindow = window.open('', '_blank', 'width=500,height=500', false);
         newWindow.focus();

        var service = new LocalProxy.LocalProxyToExternalService();
        service.InitiateTransaction($('input[id*=TextBox1]').val());   //, OnPayWithXYZCompleted, OnPayWithXYZError);
    }

    function OnPayWithXYZCompleted(result) {
        $('span[id*=Label1]').text(result);            
    }

    function OnPayWithXYZError(result) {
        alert(result.get_message());
    }

</script>


<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/Services/LocalProxyToExternalService.svc" />
    </Services>
</asp:ScriptManager>   


<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Pay with XYZ" OnClick="Button1_Click" />
    <input id="Button2" type="button" value="Client Button" onclick="PayWithXYZ();" />
</div>
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="Label"></asp:Label>

基本上,我通过单击按钮打开一个新窗口,然后想在该新窗口中处理新请求,以便我可以从我网站上的当前页面向用户显示状态

到目前为止,它打开了新窗口,但没有将请求转移到该窗口,但我在 firebug 中收到如下错误消息...

【问题讨论】:

    标签: javascript asp.net ajax wcf


    【解决方案1】:

    Web 服务本身没有任何 UI,因此您从浏览器调用的所有 Web 服务都将在后台运行 - 所以我不清楚是否需要在新窗口中执行服务 - IMO,根本不需要.

    您的错误消息表明您的网络服务 InitiateTransaction 没有返回正确的数据。该方法的实现是什么。 IMO,它应该调用外部 Web 服务 - 通过使用服务代理(如果其 SOAP 服务由添加服务引用创建)或使用 WebClient/WebRequest(如果它是基于 REST 的服务)。

    【讨论】:

    • 实际上,这个没有那种用户界面。当您单击 Process 按钮时,它会调用 LocalProxy InitiateRequest 方法,该方法基本上是调用外部 web 服务的 InitiateRequest 方法。外部 InitiateRequest 网络方法首先将页面重定向到它自己的页面,然后在那里进行支付处理。您可以认为它与 Paypal 相同,但在这里,这一切都发生在同一页面上。希望这会有所帮助
    • @DotNetInfo,外部网络服务如何重定向页面(它可能不会被浏览器调用)?它公开了什么样的 API - 是基于 SOAP/XML 的 API 还是 HTTP URL?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多