【问题标题】:Ajax POST to C# WCF WebService, 404 not foundAjax POST 到 C# WCF WebService,找不到 404
【发布时间】:2017-05-06 12:37:21
【问题描述】:

我有一个带有 javascript 和 Ajax POST 的简单 HTML 页面,用于调用我制作的 WCF WebService。

HTML 页面的作用是从用户输入中收集所有信息,将其包装在 json 中,然后调用 web 服务。

webservice 应该接收 Ajax POST,将 json 反序列化为一个对象并将其插入 DB。

我在我的本地 IIS 服务器下发布了 WCF 服务,但我收到错误 404 未找到。我还将 HTML 页面托管在不同的应用程序中,在同一个网站下,在同一个 IIS 本地主机中。

可能是什么问题?我正在尝试修复此问题 2 天,但没有成功。

这是我的代码:

Ajax 帖子:

        $.ajax({
                    type: 'POST',
                    crossDomain: true,
                    dataType: 'json',
                    url: "http://localhost/wcf_test/Service1/InsertUpdateIndividualEpxert",
                    data: JSON.stringify({individualExpert}),
                    contentType: "application/json; charset=utf-8",
                    success: onDataReceived,
                    error: onDataError
            });

function onDataReceived(data) 
{
    console.log('Everything is good!');
}
function onDataError() 
{
    console.log('Not working mister!');
}

WCF 接口:

        [OperationContract]
    [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "InsertUpdateIndividualEpxert/{json}")]
    void InsertUpdateIndividualEpxert(string json);

WCF Web.Config:

<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="Service1_HttpBinding">
    </binding>
  </webHttpBinding>
</bindings>

<behaviors>
  <endpointBehaviors>
    <behavior name="Service1_EndpointBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Service1_ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="Service1_ServiceBehavior" name="WS_Experts.Service1">
    <endpoint address=""
      behaviorConfiguration="Service1_EndpointBehavior"
      binding="webHttpBinding"
      bindingConfiguration="Service1_HttpBinding"
      contract="WS_Experts.IService1" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>

【问题讨论】:

    标签: http-status-code-404


    【解决方案1】:

    所以我只有两个问题。

    1. 在服务接口中,UriTemplate应该是“InsertUpdateIndividualEpxert”和NOT“InsertUpdateIndividualEpxert/{json} "

    2. 我写的是“Epxert”而不是“Expert”。

    我用来解决这个问题的工具是 Google Chrome 的“PostMan”(没有做任何广告,只是给出了完整的规格),它给了我可以理解的错误“找不到端点”.. . 就在那一刻,我知道我搞砸了!

    现在我收到“不允许使用 405 方法”,但我的朋友们是另一回事!

    祝大家编码愉快!

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      相关资源
      最近更新 更多