2012-05-11 31 views
0

HTTPリソースとして公開されている外部ドメインでリモート "サービス"を呼び出す必要があります。 サービスはPOST要求のみを受け入れます。POSTで古典的なaspクロスドメインHTTPリクエスト

POSTメソッドをサポートしていないため、JSONPは使用できません。 AJAXリクエストはクロスドメインリクエストのため使用できません。

些細な解決策は、ServerXMLHTTPオブジェクトを使用して要求を管理することです。欠点は、ServerXMLHTTPでは要求が同期的であることです。

+0

は、[この](http://stackoverflow.com/questions/1576040/synchronous-cross-sub-domain-post-request-with-jqueryを見てみましょう)SOスレッド。 –

答えて

1

ServerXMLHTTPは、アプリケーションでホストされているサーバー側のコードで使用されるため、同期していても、このページへの呼び出しは通常のXmlHttpを使用して非同期になる可能性があります。基本的には、ブラウザのクロスサイトスクリプティングの制限を克服するために、サーバーにプロキシを作成しています。

サーバー側: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) 
%> 

は今AJAX(jQueryのgetJSONを言う)を使用して、クライアント側からこのProxy.aspを呼び出します。したがって、サーバーがブロックしている間も、クライアントの呼び出しはまだ非同期です。

クライアント側:

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