0

我正在为我的移动 Web 应用程序构建简单的 WCF Web 服务。

我在$.ajax向 WCF 发出请求时堆栈405 - Method not allow,即使我这样做时我可以在浏览器http://http://localhost:35798/RestServiceImpl.svc/json/23中看到结果。ReturnJSONData()

我经历了 100 篇不同的帖子,但没有一个答案能解决我的问题。

AJAX 请求

$.ajax({
    type: "GET",
    url: "http://localhost:35798/RestServiceImpl.svc/json/34",
    contentType: "application/json; charset=utf-8", 
    success: function(data) {
    console.log(data);
    },
});

IRestServiceImpl.cs

namespace RestService{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(
            Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}"
            )]
        string ReturnJSONData(string id);
    }
}

RestServiceImpl.svs.cs

namespace RestService {
    public class RestServiceImpl : IRestServiceImpl {
        public string ReturnJSONData(string id) {
            return "You requested product " + id;
        }
    }
}

网络配置

<?xml version="1.0"?>
<configuration>

    <system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
    <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
        </endpoint>
    </service>
</services>

<behaviors>
    <serviceBehaviors>
        <behavior name="ServiceBehaviour">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="web">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
</behaviors>
<standardEndpoints>
    <webHttpEndpoint>
        <standardEndpoint name=""
        helpEnabled="true"
        automaticFormatSelectionEnabled="true"
        defaultOutgoingResponseFormat ="Json"
        crossDomainScriptAccessEnabled="true"/>
    </webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>

任何建议都非常感谢。

4

0 回答 0