2

私はこれで2日間立ち往生しています。

クロスドメイン AJAX を WCF サービスに投稿する方法の例を教えてください。

イメージを WCF サーバーにアップロードしようとしています。

編集

WCF サービス:

[WebInvoke(UriTemplate = "/upload", Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped), CorsEnabled]
void UploadImage(Stream image);

Ajax 呼び出し:

function UploadImage() {
        image = document.getElementById("myimage").src;
        var data = '{"image": "' + image + '"}'
        //alert(data);
        $.ajax({
            url: "http://localhost:44665/api/upload",
            type: "POST",
            contentType: "application/json",
            data: data,
            success: function (result) {
                alert("success");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(jqXHR.responseText);
            }
        });
    }

WCF パラメーターを Stream から string に変更すると、これを機能させることができます。しかし、文字列ではなく画像をアップロードする必要があります。

次のような WCF エラーが表示されます。

The server encountered an error processing the request. See server logs for more details.

** 編集 2 ** 以下の回答に記載されている global.asax コードを追加し、これを web.config に追加しました。

<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="myServiceBehavior">
                <servicedebug includeexceptiondetailinfaults="true" />
            </behavior>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>

        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"
                               aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
</configuration>

Google Chrome コンソールに次のようなエラーが表示されるようになりました。

POST http://localhost:44665/api/upload 500 (Internal Server Error) 
4

2 に答える 2

5

そのため、JavaScriptから別のドメインでホストされているWCFサービスへのPOST操作を行おうとしています。通常、WCFサービス側で特別な設定を行わずにそれを行うことはできません。

サービス側からの応答に次のヘッダーを追加する必要がありますGlobal.asax.cs(サービスプロジェクトにヘッダーが含まれていない場合は、ヘッダーをGlobal.asax.cs作成します)。

protected void Application_BeginRequest(object sender, EventArgs e)
{
   //..
   EnableCrossDomainCall();
}

private void EnableCrossDomainCall()
{
    // this header tells that from any domain you can access me.
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        // this one tells about the supported methods to client.
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
                      "GET, POST, OPTIONS");

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", 
                       "Content-Type, Accept");

        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");

        HttpContext.Current.Response.End();
    }
}

アップデート:

AJAXを介してファイルを投稿することはできません。AJAXを介して送信できるのはデータのみです。非表示のiframeを使用するこのプラグインを使用して、AJAXのように模倣するファイルをアップロードできます。

このリンクのように、WCF側でストリームオブジェクトを処理できます。最初に小さいサイズの画像をアップロードしてみてください。web.configでmaxRequestLengthを設定することで、最大サイズを制御できます。

于 2012-06-07T17:08:28.600 に答える
0

私はこの問題を抱えていて、コードを試してみましたがうまくいきませんでした。

コードを変更して作業を開始しました。

                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST,OPTIONS");

                if ((HttpContext.Current.Request.HttpMethod == "OPTIONS"))
                {

                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                    HttpContext.Current.Response.End();
                 }
于 2016-06-24T19:44:21.457 に答える