Reputation: 21
I'm trying to get a WCF service to a JSON for Jquery Ajax, but the method is not even to be initiated now returns the error Bad Request.
Below is the Java Script code calling the service, Service Interface, Class of Service, the service Web.config, Global.asax service.
Detail if I access the GetData method for $.getJSON works, but at $.Ajax not.
Already with the AddStudant method does not work somehow.
Could anyone help telling me what's wrong?
JavaScript
$("#bt").click(function () {
var url = "http://domain.net/Service1.svc/AddStudant"; var data = { "ID": 1, "Name": "Ericsson Alves" }; var jdata = {}; jdata.student = data;
callAjax(url, 'json', 'POST', function (result) { alert(result); }, JSON.stringify(jdata) ); });
function callAjax(ajaxUrl, ajaxDataType, ajaxType, funcSucess, dataValues) {
$.ajax({ url: ajaxUrl, dataType: ajaxDataType, type: ajaxType, data: dataValues, processdata: true, contentType: "application/json;charset-uf8" }) .done(function (data) { funcSucess(data); }) .always(function (data) { }).fail(function (xhr, status, errorThrown) { alert(xhr.responseText); }); }
Interface
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json)]
Student GetData(int id);
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
Student AddStudant(Student student);
}
[DataContract]
public class Student
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
}
Service Class
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json)]
public Student GetData(int id)
{
return new Student() { ID = id, Name ="Ericsson Alves" };
}
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public Student AddStudant(Student student)
{
return new Student() { ID = student.ID , Name ="Ericsson Alves" };
}
}
Web.config Service
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="WebHttpBdg" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="BehaviorDefault">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="AJAXWCFServiceAspNetAjaxBehavior">
<webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="TestWCFJsonAjax.Service.Service1" behaviorConfiguration="BehaviorDefault">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="WebHttpBdg"
behaviorConfiguration="AJAXWCFServiceAspNetAjaxBehavior"
name="Service1" contract="TestWCFJsonAjax.Service.IService1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" crossDomainScriptAccessEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<!--<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />-->
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Global.asax Service
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service1)));
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
//HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
//HttpContext.Current.Response.Cache.SetNoStore();
//EnableCrossDmainAjaxCall();
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
}
Upvotes: 1
Views: 1259
Reputation: 226
try sending data
instead of jdata
, I suppose you need to send the object like it is { "ID": 1, "Name": "Ericsson Alves" }
instead of sending it inside another object as you are currently doing {student: { "ID": 1, "Name": "Ericsson Alves" }}
Upvotes: 0