我正在开发基于webform的Web应用程序表单,我希望将JSON数据发送到一个MVC应用程序。两者都在不同的领域。
这是我的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 :(得分:0)
我可以看到你的response.d是一个数组,所以要在你的控制器动作方法上接收这些数据,你需要使用一个列表:
[HttpPost]
public ActionResult Index(List<ClientInformation> model)
{
return(View);
}
我注意到的另一件事是你发送了ajax的发布请求,而你正在使用HttpGet的动作方法,请将其更改为HttpPost。
现在,在将ClientInformation模型更改为List模型后,您将在模型对象中获得数组响应的完整数据。
我在我的应用程序中尝试通过向上述操作方法发送以下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>