我有一个简单的WCF服务,看起来像这样;
[ServiceContract]
public interface IPostService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml)]
string CheckAlive();还有..。
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PostService : IPostService
{
public string CheckAlive()
{
return "I'm here.";
}在我的MVC Web项目中,我有一个对我的Web服务的服务引用。
然后我就有了这样的jQuery代码...
function callWebMethod() {
$.support.cors = true;
$.ajax({
type: "GET",
url: "http://localhost:8732/Design_Time_Addresses/MyWebService/Service1/mex/CheckAlive",
data: "{}",
contentType: "application/soap+xml; charset=utf-8",
success: function (msg) {
alert("data:" + msg.d);
}, error: function(a,b,c){ alert("error:" + b + ":" + c); }
});
}然而,我总是得到一个"Bad Request“错误。我以前从来没有用过jQuery使用过webservice,现在我还在苦苦挣扎。
发布于 2012-02-29 13:05:34
您的postservice类必须使用[ServiceContract]装饰
namespace MyNamespace
{
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PostService : IPostService
{
public string CheckAlive()
{
return "I'm here.";
}
}
}另外,您是否在Global.ascx文件中定义了路径
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapServiceRoute<MyNamespace.PostService >("Service/SomeName");
}您可以像这样访问该服务
url:'http://loalhost:1234/Service/SomeName',https://stackoverflow.com/questions/9493687
复制相似问题