gpt4 book ai didi

javascript - 400 错误请求 - jquery WCF ajax 调用

转载 作者:行者123 更新时间:2023-11-28 08:57:35 25 4
gpt4 key购买 nike

我知道这个问题之前已经发布过多次,但即使研究这些问题的解决方案也不起作用。所以,最后我不得不发布问题:-

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

以下是服务接口(interface)代码:

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

最佳答案

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

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

binding="webHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1"

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

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

关于javascript - 400 错误请求 - jquery WCF ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18226265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com