它就像localhost上的一个魅力,但根本不在生产服务器上工作。
我的控制器方法是这样的:
[HttpPost]
public JsonResult VisualizaDebitos()
{
var filtro = Request["filtro"];
var emissao = DateTime.Parse(Request["emissao"]);
var vencimento = DateTime.Parse(Request["vencimento"]);
var mesCompetencia = int.Parse(Request["mesCompetencia"]);
var anoCompetencia = int.Parse(Request["anoCompetencia"]);
return Json(new { data = someprocesseddata }, JsonRequestBehavior.AllowGet);
}
和ajax电话:
$.ajax({
url: "/Mensalidade/VisualizaDebitos",
datatype: "json",
type: "POST",
data: {
filtro: $("#Participante").val(),
emissao: $("#Emissao").val(),
vencimento: $("#Vencimento").val(),
mesCompetencia: parseInt($("#MesCompetencia").val()),
anoCompetencia: parseInt($("#AnoCompetencia").val())
},
error: function (data) {
if (data.status == 500) {
jAlert('Response status: ' + data.status + ' (' + data.statusText + ')' +
'\n\nVerifique se a url e/ou os parâmetros estão corretos e tente novamente.', 'Error');
} else {
jAlert('Error', 'Unknown error');
}
},
success: function (result) {
console.log(result.someprocesseddata);
return false;
}
});
我收到错误500内部服务器错误
我什么都错过了?
您可以使用url:'@Url.Action("VisualizaDebitos","Mensalidade")'
而不是url: "/Mensalidade/VisualizaDebitos"
。
请尝试1件事,用完整路径替换下面的url并检查它是否有效。
url: "/Mensalidade/VisualizaDebitos",
如果它工作,那么需要使用绝对路径。
编辑: -
或者请只在post参数和test中发送字符串值。就像你发送int值一样,make it string,你可以在控制器方法中将它转换为int。
请尝试以下方法:
var RootUrl = '@Url.Content("~/")';
然后调整网址:
url: RootUrl + "Mensalidade/VisualizaDebitos",
编辑:我已经尝试了一些东西来看看如何或什么,但它似乎更多的是处理错误然后发布错误。使用以下代码:
<input type="text" id="Participante" /><br />
<input type="text" id="Emissao" /><br />
<input type="text" id="Vencimento" /><br />
<input type="text" id="MesCompetencia" /><br />
<input type="text" id="AnoCompetencia" /><br />
<button onclick="test()">gotest</button>
<script>
function test() {
$.ajax({
url: "/Home/VisualizaDebitos",
datatype: "json",
type: "POST",
data: {
filtro: $("#Participante").val(),
emissao: $("#Emissao").val(),
vencimento: $("#Vencimento").val(),
mesCompetencia: parseInt($("#MesCompetencia").val()),
anoCompetencia: parseInt($("#AnoCompetencia").val())
},
error: function (data) {
if (data.status == 500) {
alert('500');
} else {
alert('Unknown error');
}
},
success: function (result) {
alert('success');
return false;
}
});
}
</script>
以及HTTPPost中的代码,它似乎都使用以下值:Test 01-01-2016 01-01-2016 5 6
但是,如果我输入其他内容,然后在文本框中输入日期或数字,则会引发500错误。那么这个输入怎么样?格式是否正确?
var form = new FormData();
form.append("filtro", $("#Participante").val());
form.append("emissao", "$("#Participante").val());
form.append("vencimento",$("#Vencimento").val());
form.append("mesCompetencia", parseInt($("#MesCompetencia").val()));
form.append("anoCompetencia", "parseInt($("#AnoCompetencia").val()));
var settings = {
"async": true,
"crossDomain": true,
"url": "http://",
"method": "POST",
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
你在做什么是愚蠢的,我不会解释原因,但这就是你学习的方式。我建议定义一个模型,并期望该模型作为json数据发布在您的终点上,或者让你的手弄脏lil'bit https://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-helpers-forms-and-validation
尝试使用ajax代码中的以下数据参数替换数据参数:
data: {
'filtro': $("#Participante").val(),
'emissao': $("#Emissao").val(),
'vencimento': $("#Vencimento").val(),
'mesCompetencia': parseInt($("#MesCompetencia").val()),
'anoCompetencia': parseInt($("#AnoCompetencia").val())
},
不幸的是,这些回应都没有帮助我。但我认为这不是Ajax调用问题。似乎问题在于服务器或IIS更具体。经过一些更改(包括所有提示)我在网络选项卡上遇到了一些不同的错误,比如@Div说。
看图像:
更多想法?
对于我在服务器IIS上托管的应用程序,我遇到了完全相同的问题。当我尝试调用ajax post方法时,我收到了同样的错误消息。最后,我在托管服务器上使用Internet Explorer进行故障排除。
我通过在IE上禁用“显示友好的HTTP错误消息”来解决问题。禁用友好的HTTP错误消息以查看原始内容是否可以提供更多的方向。脚步:
如果你想返回实际的错误细节,比如异常细节,在一些div元素上的堆栈跟踪,
<div id="dialog" style="display: none"></div>
你可以修改你的ajax function
作为
$.ajax({
url: "/Mensalidade/VisualizaDebitos",
datatype: "json",
type: "POST",
data: {
filtro: $("#Participante").val(),
emissao: $("#Emissao").val(),
vencimento: $("#Vencimento").val(),
mesCompetencia: parseInt($("#MesCompetencia").val()),
anoCompetencia: parseInt($("#AnoCompetencia").val())
},
error: OnError
});
并将onError函数调用为:
function OnError(xhr, errorType, exception) {
var responseText;
$("#dialog").html("");
try {
responseText = jQuery.parseJSON(xhr.responseText);
$("#dialog").append("<div><b>" + errorType + " " + exception + "</b></div>");
$("#dialog").append("<div><u>Exception</u>:<br /><br />" + responseText.ExceptionType + "</div>");
$("#dialog").append("<div><u>StackTrace</u>:<br /><br />" + responseText.StackTrace + "</div>");
$("#dialog").append("<div><u>Message</u>:<br /><br />" + responseText.Message + "</div>");
} catch (e) {
responseText = xhr.responseText;
$("#dialog").html(responseText);
}
$("#dialog").dialog({
title: "jQuery Exception Details",
width: 700,
buttons: {
Close: function () {
$(this).dialog('close');
}
}
});
}
参考:This article