私はこれで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)