400错误请求 - jquery WCF ajax调用

时间:2013-08-14 08:04:41

标签: javascript jquery asp.net wcf

我知道此问题之前已多次发布,但即使是在查看这些问题的解决方案也无效。所以,最后我不得不发布这个问题: -

这是我的ajax脚本

 function CallWebservice()
 {
    var textboxvalue = $('#TextBox1').val();
        $.ajax({
            url: "http://localhos/Service1.svc/GetData",
            type: "POST",
            data: '{"value": "' + textboxvalue + '"}',
            dataType: "json",
            processdata: true,
            contentType: "application/json; charset=utf-8",
            success: function (res) { 
            alert(res);
             },
        });
   }

以下是服务接口代码:

namespace WcfService1
{
[ServiceContract]
[System.Web.Script.Services.ScriptService]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "POST",ResponseFormat = WebMessageFormat.Json)]
    string GetData(string value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
    }
 }

服务代码

   [AspNetCompatibilityRequirements(RequirementsMode=        AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    public string GetData(string value)
    {
        return (value == "Bose") ? "true" : "false";
    }
}

的web.config

   <?xml version="1.0"?>
  <configuration>
  <connectionStrings>
<add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated      Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />
 </connectionStrings>
 <system.web>
 <compilation debug="true" targetFramework="4.0" />

 <authentication mode="Forms">
  <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
 </authentication>

 <membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
  </membership>

  <profile>
  <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
  </providers>
  </profile>

 <roleManager enabled="false">
  <providers>
    <clear/>
    <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
  </providers>
  </roleManager>

   </system.web>

   <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
    <system.serviceModel>
    <behaviors>
    <endpointBehaviors>
    <behavior name="BasicHttpBinding_IService1">
      <webHttp/>
    </behavior>
    </endpointBehaviors>
    </behaviors>
    <bindings>
    <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
      </binding>
      </basicHttpBinding>
      </bindings>
      <client>
      <endpoint address="http://localhost:32560/Service1.svc" binding="webHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
      name="BasicHttpBinding_IService1" />
      </client>
      </system.serviceModel>
      </configuration>

问题:

- 代码中有什么问题吗?
- 我无法进行WCF服务电话? Firebug说400个坏请求?

1 个答案:

答案 0 :(得分:0)

请确保您能够在IIS中浏览该服务。 您看来web.config缺少<services>个节点。还有一些奇怪的事情:

您的绑定是webHttpBinding,但绑定配置是basicHttpBinding,带有用户名/密码的邮件安全性。

binding="webHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService1"

还有名为“BasicHttpBinding_IService1”的服务行为。

如果您尝试WCF REST,请创建一个新的解决方案,然后重试。