AJAX启用WCF服务功能以重定向到新窗口

时间:2011-04-01 03:19:56

标签: javascript asp.net ajax wcf

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

我想要什么:我希望当用户点击我们网站上的按钮('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中收到如下错误消息... enter image description here

1 个答案:

答案 0 :(得分:0)

这样的Web服务没有任何用户界面,因此所有来自浏览器的Web服务调用都会在后台进行 - 所以我不清楚是否需要在新窗口中执行服务 - IMO,根本不需要。

您的错误消息表明您的Web服务InitiateTransaction未返回正确的数据。该方法的实现是什么? IMO,应该调用外部Web服务 - 使用服务代理(如果是SOAP服务,由Add Service Reference创建),或者使用WebClient / WebRequest(如果它是基于REST的服务)。