2012-11-13 144 views
0

當我通過這個請求時,它傳遞null來請求服務器站點上的參數。 數據屬性有問題嗎?參數在Jsonp請求中傳遞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; } 
} 

回答

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'); 
+0

測試了這一點,但沒有benifit ..我注意到,而不是完整的對象,如果我改變api代碼接受三個參數,然後它的工作.. public HttpResponseMessage GetDomainAvailability字符串subDomain,字符串parentDomain,字符串resellerId) ________ >>>> {subDomain:subDomain,parentDomain:parentDomain,resellerId:resellerId} –

+0

因此問題與完整的對象。 –

+0

試試我的編輯。如果這仍然無效,請嘗試將.getJSON更改爲$ .post()。 –

1

嘗試了這一點,而不是:

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

仍然沒有運氣null –

+0

@SOFUser然後你做錯了什麼。 – Neal

+0

即時通訊混淆然後它如何在服務器端API調試打點調試點代碼 –