我已经搜索了这个,我一直没有找到可以帮助我的东西,所以我appologise,如果这已发布,我刚刚无法找到它。使用JQuery Mobile的WCF服务正在返回一个400错误的请求
我已经创建了一个在IIS中托管的WCF服务应用程序。目前它的基础只是一个hello世界方法,基本上是将国家名称及其代码作为json对象返回。
我也写了一些jquery,将远程调用该方法的目的是填充列表对象。
目前当我调用方法时,它会触发ajax调用的成功参数并以“undefined”提醒我我不知道是什么造成了这种情况,但它最有可能犯了一个愚蠢的错误。
继承人的服务和jQuery
web配置的代码:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="None" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true"/>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
service1.svc
<%@ ServiceHost Language="C#" Debug="true" Service="RestfulFlightWCF.Service1" codeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
service1.svc.cs {// 注意:可以使用“重构”菜单上的“重命名”命令将代码,svc和配置文件中的类名称“Service1”一起更改。
[ServiceContract(Namespace = "JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public Country GetCountry(string id)
{
Country county = new Country();
county.Name = "United Kingdom";
county.Id = "gb";
return county;
}
[DataContract]
public class Country
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
}
jQuery的
$(document).ready(
function() {
$.ajax({
type:"GET",
Data:'gb',
Url:"http://192.168.1.6:80/FlightServices.svc/GetCountry",
DataType:"jsonp",
method:"GetCountry",
success: function(msg){
debugger;
alert(msg.responseText);
if (msg.responseText) {
var err = msg.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
},
failure: function(){
alert("something went wrong");
},
error: function(){
alert("something happned");
}
});
});
对不起,长职位,但我认为这将有助于如果我包括我的代码。
看的好像你是不是序列化您的数据json –
可能是一个愚蠢的问题,但我累了,不能思考直。那是在ajax调用还是在wcf服务中,我将不得不序列化它? – gilljoy
在你返回县之前,你需要将它序列化成json ... –