[英]Bad Request Call WCF Service Jquery Ajax
我正在尝试为Jquery Ajax的JSON提供WCF服务,但是该方法甚至无法启动,现在返回错误Bad Request。
以下是调用服务,服务接口,服务类,服务Web.config,Global.asax服务的Java脚本代码。
如果访问$ .getJSON的GetData方法有效,但不能使用$ .Ajax,则详细信息。
已经使用AddStudant方法无法正常工作。
谁能告诉我这是怎么回事?
JavaScript的
$(“#bt”)。click(function(){
var url =“ http://domain.net/Service1.svc/AddStudant ”; var data = {“ ID”:1,“ Name”:“ Ericsson Alves”}; var jdata = {}; jdata.student =数据;
callAjax(url,'json','POST',函数(结果){alert(result);},JSON.stringify(jdata)); });
函数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); }); }
接口
[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; }
}
服务等级
[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服务
<?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服务
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();
}
}
}
在ajax请求中将contentType
设置为text/plain
尝试发送data
而不是jdata
,我想您需要发送的对象是{ "ID": 1, "Name": "Ericsson Alves" }
而不是像您目前正在做的那样在另一个对象中发送{student: { "ID": 1, "Name": "Ericsson Alves" }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.