.net - 使用 Jquery Mobile 使用 WCF 服务返回 400 错误请求

标签 .net ajax wcf jquery

我已经搜索过此内容,但找不到对我有帮助的内容,因此,如果此内容已发布而我只是无法找到它,我深表歉意。

我创建了一个托管在 IIS 中的 WCF 服务应用程序。目前它非常基本,只有一个 hello world 方法,基本上以 json 对象的形式返回国家/地区名称及其代码。

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

目前,当我调用该方法时,它会命中 ajax 调用的成功参数,并用“未定义”提醒我,我不知道是什么原因造成的,但很可能我犯了一个愚蠢的错误。

这是服务和jquery的代码

网络配置:

<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");
            }
        });
         });

很抱歉这篇文章很长,但我认为如果我包含我的代码将会有所帮助。

最佳答案

如何寄回的小样本

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

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

关于.net - 使用 Jquery Mobile 使用 WCF 服务返回 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13370850/

相关文章:

c# - 将 List<string[]> 展平为单个字符串,每个元素一行

.net - 工作单元实现

wcf - 为什么 ServiceBehavior 是服务类的属性而不是合约的属性?

.net - 如何使代码合约与数据合约的反序列化一起工作?

c# - WCF 多进程处理/设计

c# - 让windows决定如何打开/运行文件?

c# - 为什么将 null 传递给 params 方法会导致空参数数组?

javascript - 尝试将变量设置为 jquery ajax post 中 URL 的一部分

javascript - 如何使用 AJAX 从 php 获取值

javascript - "asp:button"上的 ASP.NET 无效回发或回调参数单击 JQuery POST