我试图使用jquery从MVC(razor)调用我的WCF方法然后我收到错误
网络配置
<system.serviceModel>
<services>
<service name="Estimate.Service.ajaxServiceEstimate" behaviorConfiguration="Estimate.Service.ajaxServiceEstimate" >
<endpoint address="" behaviorConfiguration="Estimate.Service.ajaxServiceEstimateAspNetAjaxBehavior"
binding="webHttpBinding" contract="Estimate.Service.ajaxServiceEstimate" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="Estimate.Service.ajaxServiceEstimateAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Estimate.Service.ajaxServiceEstimate" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
服务代码
namespace Estimate.Service
{
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ajaxServiceEstimate
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
// Add more operations here and mark them with [OperationContract]
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public string Location()
{
return "anurag";
}
}
}
jQuery代码:
<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function GetTeamData() {
$.ajax(
{
async: true,
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:8377/Service/ajaxServiceEstimate.svc/Location",
dataType: "json",
data: '{}',
success: function (content) {
DisplayRun(map, content);
}
});
}
</script>
<div>
<input id="btnSave" type="button" value="Save" onclick="GetTeamData();" />
<div id="divOutput"></div>
</div>
答案 0 :(得分:0)
是否有必要使用WCF。更好地使用WebAPI它们非常简单快速。如果卡在绑定中,WCF会产生问题。如果可以,请尝试使用WebAPI。
答案 1 :(得分:0)
您应该检查路由文件,并将Services文件夹设置为忽略。 还要添加要接受的文件类型。
routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.IgnoreRoute("{folder}/{*pathInfo}", new { folder = "Service" });
了解更多信息 http://forums.asp.net/t/1970859.aspx?Problem+Calling+WCF+Service+Library+from+jQuery+