Javascript 向控制器传递和检索AJAX数据
我的视图中有此代码Index.cshtml:Javascript 向控制器传递和检索AJAX数据,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我的视图中有此代码Index.cshtml: <script> var dropdown = $('#clientDropdown'); dropdown.on('change', function () { var clients = dropdown.chosen().val(); $.ajax({ type: 'GET', url: '/Inventory/GetClient',
<script>
var dropdown = $('#clientDropdown');
dropdown.on('change', function () {
var clients = dropdown.chosen().val();
$.ajax({
type: 'GET',
url: '/Inventory/GetClient',
datatype: 'json',
data: {clients : clients},
success: function (data) {
console.log(data);
}
});
});
</script>
在AJAX调用之后,控制器在控制台日志中返回一个{data:null}对象。我错过了什么?我想在我的控制器中使用我的客户机对象的内容来返回JSON数据您必须发送一个字符串版本的
JSON
,下面是您应该做的一些更改
var dropdown = $('#clientDropdown');
dropdown.on('change', function () {
var clients = dropdown.chosen().val();
$.ajax({
type: 'GET',
url: '/Inventory/GetClient',
contentType: 'application/json', // this
datatype: 'json',
data: {clients : JSON.stringify(clients)}, // and this
success: function (data) {
console.log(data);
}
});
});
你试过在你的c#代码中调试
客户端的运行时值吗?@garryman-yup。它甚至没有从ajax调用中获取数据检查我的更新回答等等。我想把它作为一个对象检索。当我记录data.data[0]时,是输出“[”console.log(JSON.parse(data)[0]);
var dropdown = $('#clientDropdown');
dropdown.on('change', function () {
var clients = dropdown.chosen().val();
$.ajax({
type: 'GET',
url: '/Inventory/GetClient',
contentType: 'application/json', // this
datatype: 'json',
data: {clients : JSON.stringify(clients)}, // and this
success: function (data) {
console.log(data);
}
});
});