我在几个不同的 ASP.NET 页面上实现了一个注册过程。这些页面本身可以工作,但是当我单击其中任何一个上的提交时,我会看到一条类似这样的消息
{"成功":true,"异常":null,"InnerException":null,"ReturnVal":3}
ReturnVal
我收到的是正确的。似乎一旦AJAX
函数完成,它就会停止并且不会继续错误或成功,它只是显示JSON
结果。这是一个片段:
<input type="submit" value="Next" id="Package" onclick="return DoAjaxSubmit(this);"/>
</div>
</div>
</p>
<script type="text/javascript">
function DoAjaxSubmit(btnClicked) {
var form = $(btnClicked).parents('form');
$.ajax({
type: "POST",
url: form.attr('action'), //'<%=ResolveUrl("~/register/go") %>',
data: form.serialize(),
dataType: "json",
error: function (xhr, status, error) {
},
success: function (response) {
if (response != null) {
if (!response.Successful) {
alert("Please select the Package");
return false;
}
else {
switch (response.ReturnVal) {
case 0:
window.location.href = "/Register/Free";
break;
case 1:
window.location.href = "/Register/Confirmation";
break;
case 2:
window.location.href = "/Register/AdOptions";
break;
case 3:
window.location.href = "/Register/PrintOptions";
break;
case 4:
window.location.href = "/Register/JobOptions";
break;
case 5:
window.location.href = "/Register/Landing";
break;
}
}
}
}
});
return false;
}
控制器:
[AcceptVerbs(HttpVerbs.Post)]
[RestrictedAction(new[] { "user" },null)]
public JsonResult Package(string[] package)
{
var t = new TransactionResult();
if (!string.IsNullOrEmpty(package[0]))
{
//Reset Session values in case of start over
Session[SessionNames.PRINTOPTIONS] = null;
Session[SessionNames.JOBOPTIONS] = null;
Session[SessionNames.ADOPTIONS] = null;
Session[SessionNames.LANDING] = null;
Session[SessionNames.QUANTITY] = null;
Session[SessionNames.PACKAGE1] = package;
//Determine which options page to navigate to
t.ReturnVal = 6;
foreach (var p in package)
{
if (Convert.ToInt32(p) < t.ReturnVal && Convert.ToInt32(p) != 1) t.ReturnVal = Convert.ToInt32(p);
}
if (t.ReturnVal == 6) t.ReturnVal = 1;
t.Successful = true;
}
return Json(t);
}
我尝试删除提交按钮上的返回值,添加 return false;,将JSON
结果的成功属性设置为 falsecontroller
以检查错误部分是否有效,但它只是做同样的事情(除了它返回 false,但不显示错误消息)。
编辑控制台错误
Uncaught ReferenceError: $ is not defined Package:283
DoAjaxSubmit Package:283
onclick Package:263
Resource interpreted as Document but transferred with MIME type application/json: "http://192.168.0.21:82/register/package".