【问题标题】:HTTP/1.1 404 Not Found (how to call wcf service from jquery)HTTP/1.1 404 Not Found(如何从 jquery 调用 wcf 服务)
【发布时间】:2014-03-04 10:48:29
【问题描述】:

我正在尝试使用 jquery 从 MVC(razor) 调用我的 WCF 方法,然后出现错误

  1. 请求网址:localhost:8377/Service/ajaxServiceEstimate.svc/Location
  2. 请求方法:POST 状态
  3. 代码:HTTP/1.1 404 未找到

网络配置

    <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>

【问题讨论】:

  • 你能在任何情况下解析 localhost:8377/Service/ajaxServiceEstimate.svc/Location 吗?
  • 嗨 NWard,我试过“~/Service/ajaxServiceEstimate.svc/Location”这个和“../Service/ajaxServiceEstimate.svc/Location”这个

标签: jquery asp.net-mvc wcf razor


【解决方案1】:

是否有强制使用 WCF。更好地使用 WebAPI,它们非常简单和快速。如果卡在绑定中,WCF 会产生问题。如果可以,请尝试使用 WebAPI。

【讨论】:

  • 这是一个可能的评论!
  • 感谢回复,实际上我必须在 MVC(razor) 中创建应用程序并从 jQuery 调用 WCF 这是必需的
【解决方案2】:

您应该检查您的路由文件,并将服务文件夹设置为被忽略。 还要添加要接受的文件类型。

            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+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2023-03-24
    相关资源
    最近更新 更多