2017-01-02 93 views
0

我正在研究基於webform的web應用程序表單,我想將JSON數據發送到一個MVC應用程序。兩者都在不同的域。使用ajax發佈JSON數據以跨域使用

這裏是我的AJAX方法:

$.ajax({ 
    type: 'POST', 
    contentType: "application/json; charset=utf-8", 
    url: 'http://localhost:52099/Controller/Index', 
    data: JSON.stringify(response.d), 
    dataType: 'jsonp', 
    success: function (response) { 
     alert("done"); 
    }, 
    error: function(e){ 
     alert(e); 
    } 
}); 

這裏是我的操作方法:

[HttpGet] 
    public ActionResult Index(ClientInformation model) 
    { 
     return(View); 
    } 

指數方法得到調用,但我沒有得到的參數值。我需要做什麼?這裏有什麼不正確的嗎?

這裏是我的數據response.d:

[{"Id":50345520,"FirstName":"Matthew","LastName":"McCauley","MiddleName":"","Suffix":null,"NPI":"1083043491","Address":"614 Esplanade St.","City":"Piscataway","State":"NJ","ZIP":"08854","Country":"United States","SubscriberFirstName":null,"SubscriberLastName":null,"SubscriberMiddleName":null,"SubscriberSSN":null,"SubscriberDOB":null,"SubscriberGender":null,"SubscriberSuffix":null,"SubscriberAddress":null,"SubscriberCity":null,"SubscriberState":null,"SubscriberZIP":null,"SubscriberCountry":null,"VisitDate":"\/Date(1483295400000)\/"}]" 

而且

public class ClientInformation 
{ 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string MiddleName { get; set; } 
    public string Suffix { get; set; } 
    public string NPI { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZIP { get; set; } 
    public string Country { get; set; } 
    public string SubscriberFirstName { get; set; } 
    public string SubscriberLastName { get; set; } 
    public string SubscriberMiddleName { get; set; } 
    public string SubscriberSSN { get; set; } 
    public DateTime? SubscriberDOB { get; set; } 
    public int SubscriberGender { get; set; } 
    public string SubscriberSuffix { get; set; } 
    public string SubscriberAddress { get; set; } 
    public string SubscriberCity { get; set; } 
    public string SubscriberState { get; set; } 
    public string SubscriberZIP { get; set; } 
    public string SubscriberCountry { get; set; } 
    public DateTime? VisitDate { get; set; } 
} 
+0

是什麼'response.d'? – CodingYoshi

+0

實際上這個ajax是在另一個ajax調用的成功之下。在response.d我收到JSON數據。 – Ankita

+0

對於初學者,您需要爲CORS服務器端實現跨越源的請求。不難研究如何實施它 – charlietfl

回答

0

我可以看到你的response.d是一個數組,所以要收到你需要你的控制器的操作方法這個數據使用列表:

[HttpPost] 
public ActionResult Index(List<ClientInformation> model) 
{ 
    return(View); 
} 

我還注意到的另一件事是,你發送一個帖子請求形式ajax和你的a您正在使用HttpGet的ction方法,請將其更改爲HttpPost。

現在將ClientInformation模型更改爲List模型後,您將在模型對象中獲得完整的數組response.d。

我試圖在我的應用程序通過發送下面的Ajax請求,以上述的操作方法,我能夠正確地接收所有數據:

<script> 
     $(document).ready(function() { 
     var r = [{ "Id": 50345520, "FirstName": "Matthew", "LastName": "McCauley", "MiddleName": "", "Suffix": null, "NPI": "1083043491", "Address": "614 Esplanade St.", "City": "Piscataway", "State": "NJ", "ZIP": "08854", "Country": "United States", "SubscriberFirstName": null, "SubscriberLastName": null, "SubscriberMiddleName": null, "SubscriberSSN": null, "SubscriberDOB": null, "SubscriberGender": null, "SubscriberSuffix": null, "SubscriberAddress": null, "SubscriberCity": null, "SubscriberState": null, "SubscriberZIP": null, "SubscriberCountry": null, "VisitDate": "\/Date(1483295400000)\/" }]; 
     $.ajax({ 
      type: 'POST', 
      contentType: "application/json; charset=utf-8", 
      url: '/Employee/Test', 
      data: JSON.stringify(r), 
      dataType: 'jsonp', 
      success: function (response) { 
       alert("done"); 
      }, 
      error: function (e) { 
       alert(e); 
      } 
     }); 

     }); 
    </script> 
+0

感謝您的回覆。但是ajax調用不會調用我的post方法。任何想法爲什麼? – Ankita

+0

@Ankita,你可能會得到控制檯錯誤! –

+0

不,沒有任何控制檯錯誤。 – Ankita