1

我知道这个问题之前已经发布过多次,但即使调查这些问题的解决方案也没有奏效。所以,最后我不得不发布这个问题: -

这是我的 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";
    }
}

网络配置

   <?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 错误请求?

4

1 回答 1

0

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

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

binding="webHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService1"

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

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

于 2014-07-03T17:14:01.047 回答