我正在尝试为Jquery Ajax获取JSON的WCF服务,但是现在甚至不启动该方法返回错误Bad Request。
以下是调用服务,服务接口,服务等级,服务Web.config,Global.asax服务的Java Script代码。
详细说明我是否访问$ .getJSON的GetData方法,但不是$ .Ajax。
已经使用AddStudant方法无法以某种方式工作。
任何人都可以帮我告诉我什么是错的吗?
的JavaScript
$(“#bt”)。click(function(){
var url =“http://domain.net/Service1.svc/AddStudant”; var data = {“ID”:1,“姓名”:“Ericsson Alves”}; var jdata = {}; jdata.student = 数据;
callAjax(url,'json','POST', function(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();
}
}
}
答案 0 :(得分:1)
在您的ajax请求中将public List<Foo> getFoos(List<Bar> bars) {
return IntStream.range(0, bars.size()) //you may add .parallel()
.mapToObj(idx -> bazUtils.getFoo(bars.get(idx), idx+1))
.collect(Collectors.toList());
}
设置为contentType
答案 1 :(得分:0)
尝试发送data
而不是jdata
,我想您需要发送对象{ "ID": 1, "Name": "Ericsson Alves" }
,而不是将其发送到您正在执行的其他对象{{1} }