2012-11-13 37 views
2

我已经搜索了这个,我一直没有找到可以帮助我的东西,所以我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"); 
      } 
     }); 
     }); 

对不起,长职位,但我认为这将有助于如果我包括我的代码。

+0

看的好像你是不是序列化您的数据json –

+0

可能是一个愚蠢的问题,但我累了,不能思考直。那是在ajax调用还是在wcf服务中,我将不得不序列化它? – gilljoy

+0

在你返回县之前,你需要将它序列化成json ... –

回答

0

好的所以我今晚在摆弄了一段时间之后才开始工作,所以我想我会在发现一些我发现的东西的同时寻找修复。

  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"} 
0

它是如何有一个小样本被送回

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

硬编码是一种不好的做法。更好地序列化数据。

+0

我试过用JavaScriptSeriliazer,它仍然返回null ,出于某种原因,当调试jquery时,查看msg变量时,它只包含整个javascript而不是一个json对象,这让我觉得我现在在做jquery的错误 – gilljoy

+0

它仍然返回一个400错误的错误请求 – gilljoy

0

看起来你可能缺少从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

+0

Didn似乎没有工作,认真考虑放弃现在的WCF – gilljoy

相关问题