简体   繁体   中英

AJAX enabled WCF Service function to redirect to a new window

Scenario : I am going to access external web service [ExternalWS] using AJAX. So obviously, need to create local proxy service [LocalProxyWS] first, which in turn will access the external web service. Now, the external service webmethod [Process] basically redirects the current page on our site to their site, does some work and then return back to our site.

What I want : I want that when the user clicks the button ('Process') on our site, it should open a new window and then starts executing the request on the new window, so that I can have the page on my website to be displayed permanently (which will poll a request every 15 seconds to the external service (via local proxy) for the status).

Hope I am clear in my explanation.

Below is my code so far, please feel free to comment if there is any other way which is an efficient alternative..

Local Proxy service which calls external service is as follows..

    [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);
    }
}

My Page code is as follows...

        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>

Basically, I am opening a new window from the button click and then want to process the new request in that new window, so that I can show the status to the user from the current page on my website

So far, it opens the new window, but doesn't transfer the request on to that, but I get an error message in firebug as follows... 在此处输入图片说明

Web Services as such don't have any UI and so all your web service calls from browser would be in back-ground - so I am not clear about the need to execute service in new window - IMO, its simply not needed.

Your error message indicates that your web service InitiateTransaction does not return correct data. What is the implementation of that method. IMO, it should be invoking a external web service - either by using service proxy (created by Add Service Reference if its SOAP service) or by using WebClient/WebRequest (if it is REST based service).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM