這個ajax從.cshtml文件調用到控制器,但getValue函數在加載時取值爲空,但之後工作正常。它平均顯示ajax調用上的警報值,但在控制器上變爲空值。任何幫助將不勝感激。函數在mvc5中取ajax的空值
<script>
$(document).ready(function(){
//get value from dropdown list
var ID = $('#cmb').val();
// ID is taking value
$.ajax({
// /ControllerName/FunctionName
// /Home/getvalue
url: "/Home/getValue",
type: "POST",
// ID is getting from dropdownList
// parameterName/value
data: JSON.stringify({'_ID':ID}),
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.status == "success") {
//if got response
//alert(data.status);
}
},
error: function() {
//if if not response
//alert('something went wrong');
}
});
});
</script>
[HttpPost]
public JsonResult getValue(string _ID)
{
string a = _ID;
}
數據: '{ 「_ID」:' + ' 「' + ID + '」}' –
可能你沒有在ID值。或嘗試使用:data:{_ ID:ID} –