Saturn K
Saturn K

Reputation: 2785

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";
        }
    }

Upvotes: 1

Views: 292

Answers (2)

Saturn K
Saturn K

Reputation: 2785

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.

Upvotes: 0

Craig White
Craig White

Reputation: 13992

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.

Upvotes: 1

Related Questions