通过 Scriptish 或 Greasemonkey 调用本地 WCF 服务

本文关键字:WCF 服务 调用 Scriptish Greasemonkey 通过 | 更新日期: 2023-09-27 18:31:20

我正在尝试公开一个本地 WCF 服务,该服务检查我的数据库中是否存在可从脚本脚本访问的文件。

是否可以从 Scriptish 或 Greasemonkey(GET 或 POST)调用本地 URL?我在本地计算机上创建了一个托管在 IIS 中的 WCF 服务,该服务工作正常。但是,当我尝试从脚本调用该服务时,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");
}

我觉得我已经尝试了一切。任何帮助将不胜感激!

通过 Scriptish 或 Greasemonkey 调用本地 WCF 服务

我想出了如何通过GET请求来做到这一点,感谢M.Babcock将我推向那个方向(为了节省空间而故意省略了不重要的部分)。

Service.svc:

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

网络配置:

<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>    

以下是在脚本中执行此操作的方法:

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!");
    }
});

Plain ol' jQuery:

$.get("service", { partialFilename: filename }, function (result) {
    if (result == true) {
        videoFrame.remove(); 
    }
});