gpt4 book ai didi

javascript - 无法将 JSON 结果传递回 View

转载 作者:行者123 更新时间:2023-11-28 13:28:11 24 4
gpt4 key购买 nike

我有以下 HTML 来显示一些消息

...
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<span id="fail_message" class="label label-danger"></span>
<span id="success_message" class="label label-success"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="button" value="Invite User" class="btn btn-primary"/>
</div>
</div>
...

按下按钮时触发的脚本是

<script>
$(function () {
$("input[type=button]").click(function () {
var data_email = $('#email').val();
var data_product = $('#product option:selected').text();
$.ajax({
url: 'Tools/SendInvite',
type: 'POST',
data: { email: data_email, product: data_product },
success: function (result_success, result_failure) {
$('#fail_message').val(result_failure);
$('#success_message').val(result_success);
}
});
});
});
</script>

然后在我的 Controller 中我有

[AllowAnonymous]
public async Task<ActionResult> SendInvite(
string email, string product)
{
// Check if admin.
ApplicationUser user = null;
if (ModelState.IsValid)
{
user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
if (user.IsAdmin != null && (bool)user.IsAdmin)
{
string key = String.Empty;
string subMsg = String.Empty;
var accessDB = new AccessHashesDbContext();
switch (product) { /* do stuff */ }

// Send email.
try
{
await Helpers.SendEmailAsync(new List<string>() { email }, null, "my msg string" );
result = String.Format("Invitation successfully sent to \"{0}\"", email);
return Json(new {
result_success = result,
result_failure = String.Empty
},
JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
result = String.Format("Invitation to \"{0}\" failed", email);
return Json(new {
result_success = String.Empty,
result_failure = result
},
JsonRequestBehavior.AllowGet);
}
}
}
return Json(new {
result_success = String.Empty,
result_failure ="Invite Failure"
},
JsonRequestBehavior.AllowGet);
}

Controller 方法触发,电子邮件已发送并且 View 返回,但我的消息未显示。 如何让标签显示正确的文本?

感谢您的宝贵时间。

<小时/>

注意。我见过

  1. ASP.NET MVC controller actions that return JSON or partial html
  2. how to set textbox value in jquery

但是这些对我没有帮助。

最佳答案

JavaScript 需要是这样的:

<script>
$(function () {
$("input[type=button]").click(function () {
var data_email = $('#email').val();
var data_product = $('#product option:selected').text();
$.ajax({
url: 'Tools/SendInvite',
type: 'POST',
data: { email: data_email, product: data_product },
success: function (result) {
$('#fail_message').html(result.result_failure);
$('#success_message').html(result.result_success);
}
});
});
});
</script>

请注意,success 函数显示仅传入一个参数:result

这是因为在您的操作中您返回了以下内容:

return Json(new {
result_success = String.Empty,
result_failure ="Invite Failure"
},
JsonRequestBehavior.AllowGet);

您创建一个新对象:new {},然后用一些字段填充它。因此,当它被发送回页面时,将出现一个具有两个属性的对象 (result)。显然,返回的对象不称为 result,但这是在函数上下文中为其指定的名称。

最后,由于您使用的是 return Json,您还应该确保您的 Action 方法返回类型 JsonResult 而不是 ActionResult。在底层,ASP.NET 团队扩展了 ActionResult,但他们还设置了一些属性,例如内容类型,以确保网页知道如何处理响应。它还使用 JavaScript 序列化程序来正确序列化对象。引用:http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/JsonResult.cs

public async Task<JsonResult> SendInvite(
string email, string product)
{

如果您想使用 newtonsoft JSON.net,我将创建实现它的自定义结果。

关于javascript - 无法将 JSON 结果传递回 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27382709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com