0

Scriptish スクリプトからアクセスできるファイルがデータベースに存在するかどうかを確認するローカル WCF サービスを公開しようとしています。

Scriptish または Greasemonkey (GET または POST) からローカル URL を呼び出すことはできますか? ローカル マシンの IIS でホストされる WCF サービスを作成しましたが、サービスは正常に動作しています。ただし、Scriptish からサービスを呼び出そうとすると、Chrome/Firefox の [ネットワーク] タブに次のように表示されます。

Request URL: http://localhost/service/service.svc/MatchPartial
Request Method: OPTIONS
Status code: 405 Method Not Allowed

これが私のajax呼び出しです:

$.ajax({
    url: 'http://localhost/service/service.svc/MatchPartial',
    type: 'POST',
    contentType: 'application/json; charset=UTF-8',
    dataType: 'json',
    processData: true,
    data: '{ "partialFilename": "testing" }',
    success: function (result) {
        console.log(result);
    }
});

私のメソッドは次のように装飾されています:

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public int MatchPartial(string partialFilename)
{
    ...
}

サービスクラスの上に次のものがあります。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

私は自分のサービスに以下を追加しようとしましたが、うまくいきませんでした:

[WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
public void GetOptions()
{
    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
}

私はすべてを試したような気がします。どんな助けでも大歓迎です!

4

1 に答える 1

0

M.Babcock が私をその方向に押してくれたおかげで、GET リクエストを介してそれを行う方法を見つけました (スペースを節約するために重要でない部分を意図的に省略しました)。

Service.svc:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public bool MatchPartial(string partialFilename)
    {
        ...
    }
}

Web.config:

<configuration>
  ...
  ...
  <system.web>
    <compilation debug="true"
                 targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <!-- IMPORTANT FOR THIS TO WORK USING JQUERY GET OR AJAX -->
        <add name="Access-Control-Allow-Origin" 
             value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
  <system.serviceModel>
    <services>
      <service name="MyNamespace.Services.WCF.Service">
        <endpoint address=""
                  binding="webHttpBinding" 
                  bindingConfiguration=""
                  contract="MyNamespace.Core.Interfaces.IService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/Service" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <!-- For Debugging --->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                               multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>    

Scriptish でそれを行う方法は次のとおりです。

var service = "http://localhost/service/service.svc";

GM_xmlhttpRequest({
    method: "GET",
    url: service + "/MatchPartial?partialFilename=" + filename,
    headers: { "Accept": "application/json" },
    onload: function (result) {
        if (result != null && result.status == 200 && result.responseJSON == true) {
            videoFrame.remove(); 
        }
    },
    onerror: function (res) {
        GM_log("Error!");
    }
});

普通の jQuery:

$.get("service", { partialFilename: filename }, function (result) {
    if (result == true) {
        videoFrame.remove(); 
    }
});
于 2013-09-14T12:13:59.773 に答える