使用Jquery Mobile使用WCF服务将返回一个400错误的请求



我已经搜索过了,但找不到对我有帮助的东西,所以我很抱歉这篇文章已经发布了,但我就是找不到。

我已经创建了一个承载在IIS中的WCF服务应用程序。目前,它非常基本,只有一个helloworld方法,基本上是以json对象的形式返回国家名称及其代码。

我还编写了一些jquery,它将远程调用该方法,目的是填充列表对象。

目前,当我调用该方法时,它会命中ajax调用的成功参数,并用"未定义"提醒我。我不知道是什么导致了这种情况,但很可能我犯了一个愚蠢的错误。

这是服务和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>

服务1.svc

<%@ ServiceHost Language="C#" Debug="true" Service="RestfulFlightWCF.Service1" codeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"  %>

service1.svc.cs{//注意:您可以使用"Refactor"菜单上的"Rename"命令来更改代码、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");
}
});
});

很抱歉发了这么长的帖子,但我认为如果我包含我的代码会有所帮助。

如何将其发送回的小样本

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;
}​

硬编码是一种糟糕的做法。最好序列化数据。

看起来您可能在xml配置中缺少一些配置,包括:

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
</behavior>  
</serviceBehaviors>
</behaviors> 
<system.serviceModel>

我自己并没有真正使用WCF,但运行本教程,它应该涵盖缺少的内容:http://www.codeproject.com/Articles/417629/Support-for-JSONP-in-WCF-REST-services

好的,所以我在花了一段时间之后,今晚就完成了这项工作,所以我想把我在寻找修复程序时发现的一些东西发布出来。

  1. 在被调用的方法上向WebGet添加响应格式。

    [WebGet(ResponseFormat= WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    
  2. 将我的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();
    }
    

我发现的关键点是将参数作为传递给webGet方法

data: {id : "gb"}

最新更新