Alberto De Caro
Alberto De Caro

Reputation: 5213

classic asp cross domain http request with POST

I need to call a remote "service" in an external domain exposed as a HTTP resource. The service accepts only POST requests.

So, I cannot use JSONP as it does not support POST method. I cannot use AJAX request as it's a cross domain request.

The trivial solution is to use a ServerXMLHTTP object to manage the request. The drawback is that with ServerXMLHTTP the request is synchronous.

Any idea?

Upvotes: 0

Views: 2891

Answers (1)

amit_g
amit_g

Reputation: 31270

ServerXMLHTTP is going to be used in the server side code that is hosted by your app so even if it is synchronous, it should matter to your app as call to this page could be async using regular XmlHttp. Essentially you are creating a proxy in your server to overcome the cross-site scripting limitation of the browser.

Server side: Proxy.asp

<%    
    Function RemoteMethod1(ByVal param1, ByVal param2)
        'Use ServerXMLHttp to make a call to remote server

        RemoteMethod = ResultFromRemoteServer
    End Function

    Function RemoteMethod2(ByVal param1, ByVal param2)
        'Use ServerXMLHttp to make a call to remote server

        RemoteMethod = ResultFromRemoteServer
    End Function

    'Read QueryString or Post parameters and add a logic to call appropriate remote method

    sRemoteMethodName = Request.QueryString("RemoteMethodName")

    If (sRemoteMethodName = RemoteMethod1) Then
        results = RemoteMethod1(param1, param2)
    Else If (sRemoteMethodName = RemoteMethod2) Then
        results = RemoteMethod1(param1, param2)
    End If

    'Convert the results to appropriate format (say JSON)

    Response.ContentType = "application/json"
    Response.Write(jsonResults)
%>

Now call this Proxy.asp from your client side using AJAX (say jQuery's getJSON). So while your server is blocking, the client's call still is async.

Client side:

$.getJSON('proxy.aspx?RemoteMethodName=RemoteMethod1&Param1=Val1&Param2=Val2', function(data) {
    // data should be jsonResult
});

Upvotes: 1

Related Questions