参数在Jsonp请求中传递null

时间:2012-11-13 14:54:34

标签: c# jquery json

当我传递此请求时,它会将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; }
}

2 个答案:

答案 0 :(得分:1)

试试这个:

data: {"request":'{"SubDomain":"asfsadf","ParentDomain":"asfasdf","ResellerId":"asfdsd"}'},

答案 1 :(得分: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');