首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Scriptish或Greasemonkey调用本地WCF服务

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

Stack Overflow用户
提问于 2013-09-14 03:00:43
回答 1查看 216关注 0票数 0

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

是否可以从Scriptish或Greasemonkey (GET或POST)调用本地URL?我在本地机器上创建了一个托管在IIS中的WCF服务,该服务运行良好。然而,当我试图从Scriptish调用Chrome/Firefox中的Network选项卡时,只需说明以下几点:

代码语言:javascript
复制
Request URL: http://localhost/service/service.svc/MatchPartial
Request Method: OPTIONS
Status code: 405 Method Not Allowed

这里是我的ajax调用:

代码语言:javascript
复制
$.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);
    }
});

我的方法有:

代码语言:javascript
复制
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public int MatchPartial(string partialFilename)
{
    ...
}

以下是我的服务班:

代码语言:javascript
复制
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

我尝试在我的服务中添加以下内容,但没有运气:

代码语言:javascript
复制
[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");
}

我觉得我什么都试过了。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-14 12:13:59

我想出了如何通过GET请求来完成这个任务,这要感谢M.Babcock将我推向了这个方向(为了节省空间,故意忽略了一些不重要的部分)。

Service.svc:

代码语言:javascript
复制
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public bool MatchPartial(string partialFilename)
    {
        ...
    }
}

Web.config:

代码语言:javascript
复制
<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中如何做到这一点:

代码语言:javascript
复制
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:

代码语言:javascript
复制
$.get("service", { partialFilename: filename }, function (result) {
    if (result == true) {
        videoFrame.remove(); 
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18797857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档