Я знаю, что этот вопрос был опубликован несколько раз раньше, но даже поиск решения этих вопросов не помог. Таким образом, в конце концов я должен был отправить вопрос: -400 плохой запрос - jQuery WCF ajax call
Это мой Аякса сценарий
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 плохих запросов?
url: "http: //localhost/Service1.svc/GetData", неправильное использование localhost – Amit
Да, я знаю, что я сделал это так, потому что stackoverflow давал мне предупреждение! –
Что происходит, если вы перейдете к своей службе [http: // localhost: 32560/Service1.svc] (http: // localhost: 32560/Service1.svc) – Martin