我有一个简单的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项目中,我有一个服务引用到我的webservice。
然后我有这样的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); }
});
}
但是我总是收到“错误请求”错误。我之前从未使用过jQuery的web服务,现在正在挣扎。
答案 0 :(得分:1)
嘿,您必须检查WCF服务的端点配置。你可以看到这篇文章的详细信息 - http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery
答案 1 :(得分:0)
您的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',