0

当我传递此请求时,它将 null 传递给服务器站点上的请求参数。数据属性有问题吗?

$.getJSON('http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf&callback=?', { "request": '{"SubDomain":"asfsadf","ParentDomain":"asfasdf","ResellerId":"asfdsd"}' }, function (results) {

    alert('Cross domain JS call achieved. Have your implementation going in here!');
});

API C#

[HttpGet]
public HttpResponseMessage GetDomainAvailability(GetDomainAvailabilityRequest request)
{
    if (ModelState.IsValid)
    {
        if (request == null) return Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Request");
        var domain = string.Format("{0}.{1}", request.SubDomain, request.ParentDomain);

        var manager = new CloudSitesManager();
        var isDomainAvailable = manager.GetDomainAvailability(domain);

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, isDomainAvailable);
        return response;
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }
}

[Serializable]
public class GetDomainAvailabilityRequest
{
    public string SubDomain { get; set; }
    public string ParentDomain { get; set; }
    public string ResellerId { get; set; }
}
4

2 回答 2

1

试试这个:

data: {"request":'{"SubDomain":"asfsadf","ParentDomain":"asfasdf","ResellerId":"asfdsd"}'},
于 2012-11-13T14:55:59.667 回答
0

这是模型绑定的问题。从尝试这个开始:

$.getJSON('http://127.0.0.1:81/api/sites/GetDomainAvailability', {SubDomain:"asfsadf",ParentDomain:"asfasdf",ResellerId:"asfdsd"}, function (results) {

    alert('Cross domain JS call achieved. Have your implementation going in here!');
});

如果上面的方法不起作用,试试这个。我从一些代码中获取了这个,我正在做你正在尝试的完全相同的事情。

var data = { SubDomain: "asfsadf", ParentDomain: "asfasdf", ResellerId: "asfdsd" };

$.post('http://127.0.0.1:81/api/sites/GetDomainAvailability', data, function (results) {

    alert('Cross domain JS call achieved. Have your implementation going in here!');
}, 'json');
于 2012-11-13T15:22:13.360 回答