【发布时间】:2014-04-10 17:04:47
【问题描述】:
我有一个启动 WCF 服务的控制台应用程序,我想使用 javascript 在 html 文件中访问它。
不想使用 web.config,因为它看起来太复杂了。我想稍后将服务托管在应用程序的插件中。 (不过如果web.config符合我的要求,也可以用)。
以下是服务代码:
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080");
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
host.AddServiceEndpoint(typeof(IHelloWorldService), new BasicHttpBinding(), "bh");
host.AddServiceEndpoint(typeof(IHelloWorldService), new WebHttpBinding(WebHttpSecurityMode.None), "wb");
host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
host.Open();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
host.Close();
}
}
}
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
[WebGet(UriTemplate = "/SayHello?name={name}", ResponseFormat = WebMessageFormat.Json)]
string SayHello(string name);
}
public class HelloWorldService : IHelloWorldService
{
public string SayHello(string name)
{
Console.WriteLine("called SayHello");
return string.Format("Hello, {0}", name);
}
}
我想使用 javascript 从单个 html 文件访问服务,例如index.html 像这样:
jQuery.post("http://localhost:8080/HelloWorldService.svc/wb/SayHello", {name:"kii"}, function(ret){alert(ret);}});
或者像这样:
jQuery.get("http://localhost:8080/HelloWorldService.svc/wb/SayHello?name=kii", function(ret){alert(ret);}});
但它们不起作用。
“POST”方法得到“404 Not Found” 和 “GET”方法得到“405 Method Not Allowed”
有什么建议吗?
非常感谢~~
【问题讨论】:
-
您能出示一下 web.config 文件吗?必须了解您的行为和端点配置,以便为您提供有用的答案。
-
jQuery.get("localhost:8080/SayHello?name=kii", function(ret){alert(ret);}});
-
@PatchesTheClown,我没有 web.config 文件,因为我要将服务嵌入到应用程序插件中,不知道如果使用 web.config 是否仍然有效。
-
@yyou,不,这不起作用,仍然得到“405 Method Not Allowed”
标签: javascript wcf