[英]Consuming WCF service with Jquery Mobile is returning a 400 bad request
I've searched for this and I've not been able to find something that helps me so I appologise if this has been posted and I've just been unable to find it. 我已经搜索过此内容,但找不到能够帮助我的内容,因此我很抱歉是否已发布该内容,但我一直找不到它。
I've created a WCF service application that is being hosted in IIS. 我创建了一个由IIS托管的WCF服务应用程序。 Presently its very basic with just a hello world method basically to return a country name and its code as a json object.
目前,它的基本用法只是一个hello world方法,基本上是将国家/地区名称及其代码作为json对象返回。
I've also written some jquery that will call the method remotely with the aim of populating list objects. 我还写了一些jquery,它将远程调用该方法,目的是填充列表对象。
Currently when I call the method it hits the success paramater of the ajax call and alerts me with "undefined" I've no idea whats causing this but its most likely I've made a silly mistake. 当前,当我调用该方法时,它达到了ajax调用的成功参数,并用“未定义”警告我,我不知道是什么原因导致的,但很可能是我犯了一个愚蠢的错误。
Heres the code of the service and jquery 这是服务和jquery的代码
web config: 网络配置:
<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 service1.svc
<%@ ServiceHost Language="C#" Debug="true" Service="RestfulFlightWCF.Service1" codeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
service1.svc.cs { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 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 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");
}
});
});
Sorry for the long post but I thought it would help if I included my code. 抱歉,很长的帖子,但是我认为如果包含我的代码会有所帮助。
A small sample of how it has to be sent back 关于如何将其寄回的小样本
public string GetCountry(string id)
{
string json = string.Empty;
Country county = new Country();
county.Name = "United Kingdom";
county.Id = "gb";
json = "{ \"name\" : \"" + county.Name + "\", \"id\" : \"" + county.Id + "\" }"
return json;
}
Hard Coding is a bad practice . 硬编码是一种不好的做法。 Better to serialize the data.
更好地序列化数据。
Looks like you might be missing some configuration from the xml config including: 看起来您可能缺少xml配置中的某些配置,包括:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<system.serviceModel>
Don't really use WCF myself but run through this tutorial and it should cover whats missing: http://www.codeproject.com/Articles/417629/Support-for-JSONP-in-WCF-REST-services 自己不要真正使用WCF,而是运行本教程,它应该涵盖缺少的内容: http : //www.codeproject.com/Articles/417629/Support-for-JSONP-in-WCF-REST-services
Ok So I got this working tonight after fiddling about for a while so I thought I'd post up some of the stuff that I found out whilst looking for the fix. 好的,所以我经过一段时间的研究后,今晚开始进行这项工作,所以我想我会把我发现的一些东西发布出来,以寻找解决方法。
Add in response format to the WebGet on the method being called. 将响应格式添加到正在调用的方法的WebGet上。
[WebGet(ResponseFormat= WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
Changed my jQuery to the following: 将我的jQuery更改为以下内容:
var Type; var Url; var Data; var ContentType; var DataType; var ProcessData; var method; //Generic function to call WCF Service function CallService() { $.ajax({ type: Type, //GET or POST or PUT or DELETE verb url: Url, // Location of the service data: Data, //Data sent to server contentType: ContentType, // content type sent to server dataType: DataType, //Expected data format from server processdata: ProcessData, //True or False success: function (msg) {//On Successful service call ServiceSucceeded(msg); }, error: ServiceFailed// When Service call fails }); } function ServiceFailed(xhr) { alert(xhr.responseText); if (xhr.responseText) { var err = xhr.responseText; if (err) error(err); else error({ Message: "Unknown server error." }) } return; } function ServiceSucceeded(result) { if (DataType == "jsonp") { debugger; resultObject = result.GetEmployeeResult; var string = result.Name + " \\n " + result.Id ; alert(string); } } function GetCountry() { Data = {id : "us"}; Type = "GET"; Url = "http://192.168.1.6:80/FlightService.svc/GetCountry"; DataType = "jsonp"; ContentType = "application/json; charset=utf-8"; ProcessData = false; method = "GetCountry"; CallService(); } $(document).ready( function () { GetCountry(); }
key point I found was passing the parameters down to the webGet method as 我发现的关键点是将参数向下传递给webGet方法
data: {id : "gb"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.