我使用jquery ajax調用控制器動作(請參閱下面的代碼),執行動作後,引發了無效的Json錯誤。我注意到它會返回頁面內容。爲什麼會發生。任何人都可以幫我解決這個問題嗎?無效的Json數據返回頁面內容
控制器
[HttpGet]
public ActionResult ViewDetails(int id)
{
var eventsdetails = _service.GeteventByID(id);
return View("EventDetails",eventsdetails);
}
[HttpPost]
public ActionResult UpdateAnswers(string answers, string question, string controlid, int eventid)
{
var replacetext=string.Empty;
if (answers.Length>0)
replacetext = answers.Replace("\n", ",");
_service.UpdateAnswers(eventid, replacetext, controlid);
return RedirectToAction("ViewDetails", new { id = eventid });
}
的Javascript
function dropdownlist(controlid, title, answers, eventid) {
var $answersreplaced = answers.replace(/\,/g , " \r");
var $deleteDialog = $('<div><textarea id="answerlist" rows="10" cols="50">' + $answersreplaced + '</textarea><div><div style="font-size:9px">(To change back to an open answer field, delete all choices above and save)</div>');
$deleteDialog.dialog({
resizable: false,
height: 280,
width: 350,
title: title + " - Edit Choices",
modal: true,
buttons: {
"Save": function() {
$.ajax({
type: 'POST',
url: '@Url.Action("UpdateAnswers")',
dataType: 'json',
data: { answers: $('#answerlist').val(),
question: title,
controlid: controlid,
eventid: eventid
},
success: function (result) {
$(this).dialog("close");
alert(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
// alert('there was a problem saving the new answers, please try again');
}
});
},
Cancel: function() {
$(this).dialog("close");
}
}
});
};
謝謝,但是當我返回一個JasonResult時,我遇到了內部服務器錯誤,從中我不知道如何弄清楚,因爲錯誤是非常具體的。有任何想法嗎? – user335160
使用谷歌瀏覽器並查看控制檯/網絡選項卡,它應該更詳細地解釋錯誤 – CallumVass
好的,謝謝,我會讓你知道結果。 – user335160