Asp.net MVC 2 - 返回的 Json 对象没有在 JavaScript 中被拾取?
更新:我已经完全删除了 datatype="html" 并希望返回值始终得到评估 - 但现在什么也没有发生。
我还添加了调用“打开模态”功能。它位于 Site.Master 中包含的 LogOn UserControl 中。希望这能解决一些问题
看起来控制器没有将 json 对象返回给 ajax 调用者,有些东西在某处丢失了 - 我永远不会遇到那个断点..它完全跳过它
我有一个简单的登录模式,所以我像这样调用 LogOn Action:
<div id="logonForm">
<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions{UpdateTargetId = "logonForm"})) { %>
//my login form
<% } %>
</div>
并且该 Action 看起来像这样:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
//success login
if (Request.IsAjaxRequest())
return Json(new { Url = Url.Action("Index", "Home") }, JsonRequestBehavior.AllowGet);
else
return RedirectToAction("Index", "Home");
}
此时,它返回带有 Url 的新 Json 对象到用户控件(在 Site.Master 中),到这里的javascript:
<script type="text/javascript">
function openModel() {
$.ajax({
type: "get",
url: '/Account/LogOn',
contentType: "application/x-www-form-urlencoded;charset=utf-8",
// async: true,
success: function (result) {
debugger;
if (typeof result == 'string') {
$.modal(result, {
closeHTML: "",
containerId: "login-container",
overlayClose: true
});
}
else if (result.Url) {
debugger;
window.location.href = result.Url;
}
}
});
}
</script>
<% if (Request.IsAuthenticated) { %>
Welcome <b><%: Page.User.Identity.Name %></b>!
<%: Html.ActionLink("Log Out", "LogOff", "Account") %>
<% } else { %>
<a href="javascript:openModel()">Log In</a>
<% } %>
...但是javacript永远不会捕获它..我看不到“结果”中的内容,屏幕呈现“{Url:/}” - 只是奇怪的行为
我错过了什么?调试器;当我第一次单击链接时,会触发中断,但是当我尝试提交表单时,它会通过登录控制器 - 尝试返回 json 对象,但永远不会返回到 javascript
非常感谢
Update: I've removed the datatype="html" completely and hoping the return value will always get evaluated - but right now nothing happens.
I've also added the code that calls the "openModal" function. It is in the LogOn UserControl contained in the Site.Master. Hope this clears up a few things
It seems like from the Controller is not returning the json object back to the ajax caller, something is lost somewhere - i am never hitting that break point..it skips it entirely
I have a simple modal for logins, so i call the LogOn Action like this:
<div id="logonForm">
<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions{UpdateTargetId = "logonForm"})) { %>
//my login form
<% } %>
</div>
And the Action looks like this:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
//success login
if (Request.IsAjaxRequest())
return Json(new { Url = Url.Action("Index", "Home") }, JsonRequestBehavior.AllowGet);
else
return RedirectToAction("Index", "Home");
}
At this point it retursthe new Json object with the Url to the User Control (in Site.Master), to the javascript here:
<script type="text/javascript">
function openModel() {
$.ajax({
type: "get",
url: '/Account/LogOn',
contentType: "application/x-www-form-urlencoded;charset=utf-8",
// async: true,
success: function (result) {
debugger;
if (typeof result == 'string') {
$.modal(result, {
closeHTML: "",
containerId: "login-container",
overlayClose: true
});
}
else if (result.Url) {
debugger;
window.location.href = result.Url;
}
}
});
}
</script>
<% if (Request.IsAuthenticated) { %>
Welcome <b><%: Page.User.Identity.Name %></b>!
<%: Html.ActionLink("Log Out", "LogOff", "Account") %>
<% } else { %>
<a href="javascript:openModel()">Log In</a>
<% } %>
...but the javacript never catches it..i cannot see whats in the "result", the screen renders an "{Url: /}" - just weird behavior
What am i missing? the debugger; break is triggered when i first click the link, but when i try to submit form, it goes thru the LogOn Controller - tries to return the json object, but never goes back to the javascript
Thanks much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JQuery 不会将调用视为返回 json,因为您已告诉它调用正在返回
html
。您需要将dataType
更改为json
。但是,尚不清楚您希望从哪里调用
openModel
。代码本身将使用 Microsoft AJAX 代码并将返回的数据插入到页面中,这就是您看到 json 出现在页面上的原因。JQuery will not treat the call as returning json, because you have told it the call is returning
html
. You need to change yourdataType
tojson
.However it isn't clear where you expect
openModel
to be called from. The code as it is will use the Microsoft AJAX code and insert the returned data into the page, which is why you are seeing your json appear on the page.您需要覆盖提交点击来调用自定义 jQuery ajax 函数。现在它将使用微软的ajax。这就是为什么你的 ajax 函数永远不会被调用的原因。
做类似的事情:
You need to overwrite the submit click to call your custom jQuery ajax function. Right now it will use the Microsoft ajax. This is why your ajax function never gets called.
Do something like:
dataType: "html",
应该是dataType: "json",
,这应该可以解决问题。dataType: "html",
should be bedataType: "json",
, and this should solve the problem.好吧,jQuery 永远不会触发
success
回调,因为您指定了dataType: "html",
,因此它将等待text/html
响应,但您正在发送 JSON。您需要使用
"json"
作为数据类型。请参阅: http://api.jquery.com/jQuery.ajax/
Well jQuery will never trigger the
success
callback, since you specifieddataType: "html",
, so it will wait for antext/html
response, but you're sending JSON.You need to use
"json"
as the dataType.See: http://api.jquery.com/jQuery.ajax/