简体   繁体   中英

The following code always fails with 400 bad request

    function SendEditCommand()
    {
        jQuery.ajax({
            url: 'http://localhost:15478/Service.svc/GetTest',
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                alert('success');
            },
            error: function(request, status, error) {
                alert(error);
            }
        });
    }

    jQuery(document).ready(function () {
        SendEditCommand();
    });
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <connectionStrings>
        <add name="Entities" connectionString="metadata=res://*/Data.TechieCMS.csdl|res://*/Data.TechieCMS.ssdl|res://*/Data.TechieCMS.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost\mssql2008;Initial Catalog=TechieCMS;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
    <system.serviceModel>
        <services>
            <service name="DefaultService" behaviorConfiguration="DefaultServiceBehavior">
                <endpoint address="" binding="webHttpBinding" contract="Techie.CMS.Business.ContentProvider" behaviorConfiguration="DefaultEndpointBehavior" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
                <behavior name="DefaultEndpointBehavior">
                    <enableWebScript />
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="DefaultServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    </system.serviceModel>
</configuration>
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ContentProvider
    {
        [OperationContract]
        [WebGet()]
        public string GetTest()
        {
            return "Test";
        }
    }

Im guessing that it does not like the port within the URL. Everything else is correct. Are you using IE? Does it work with other browsers? Other people with the same problem have been using IE and it works on Firefox.

Perhaps try adding a data variable and changing it to POST? Some say that solves it.

Thank you all for your answers, they were surely relevant and a piece to the of the puzzle. The key problem was <service name="DefaultService"... I thought this was just supposed to be a name that I choose. Found out that it has to be the fully qualified name of the class that implements the contract.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM