【发布时间】:2013-09-14 20:13:59
【问题描述】:
我正在尝试公开一个本地 WCF 服务,该服务会检查我的数据库中是否存在可以从 Scriptish 脚本访问的文件。
是否可以从 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");
}
我觉得我已经尝试了一切。任何帮助将不胜感激!
【问题讨论】:
-
您是否尝试过对您的操作使用 HTTP GET 操作?从这些环境中调用会更简单。
-
@M.Babcock 是的,我试过做一个 GET,但直到你问我我才意识到我的 ajax 代码搞砸了一个 GET 请求。这是工作代码: $.get("localhost/service/service.svc/MatchPartial", { partialFilename: "testing" });
标签: c# ajax wcf userscripts scriptish