试图在asp.net mvc上学习ajax,但不知道它有什么问题吗?
我的控制器类方法=>
[HttpPost]
public JsonResult Getdata(string batchcode)
{
///var studentid = db.Studentassignbatches.Where(x => x.batch_code == batchcode).Select(x => new Studentassignbatche { UserId = x.UserId }).ToList();
return Json("hi",JsonRequestBehavior.DenyGet);
}
和我的观点和ajax请求>
<select onchange="ShowBatchCode()" id="BatchList" class="form-control input-lg">
<option class="pull-left" value="CCNA Security-1">CCNA Security-1</option>
<option class="pull-left" value="CCNA Security-2">CCNA Security-2</option>
<option class="pull-left" value="JNCSP-SEC-1">JNCSP-SEC-1</option>
<option class="pull-left" value="Oracle Database 12c-1">Oracle Database 12c-1</option>
</select>
和ajax =&gt;
<script type="text/javascript">
function ShowBatchCode() {
var batchcode = $('#BatchList').val();
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: 'Getdata',
data: { batchcode: batchcode },
success: function (data) {
alert(data);
},
error: function (result) {
alert('Something Went Wrong!');
}
});
}
它总是向我显示警告&#34;某些事情错了!&#34;。所以我调试我的网页,我发现=&gt;
505内部服务器Error.i不知道如何解决它。并尝试删除 [Httppost]并尝试使用
data: batchcode,
但没有任何对我有用。请帮助我吗?任何人!!!!!
答案 0 :(得分:2)
您已表明:
contentType: 'application/json',
但您在此处发送了application/x-www-form-urlencoded
:
data: { batchcode: batchcode },
因此要么摆脱contentType: 'application/json'
行,要么确保将JSON发送到服务器:
data: JSON.stringify({ batchcode: batchcode }),