gpt4 book ai didi

jquery - Ajax.BeginForm 返回未定义的 JSON(开发工具显示正在返回 JSON)

转载 作者:行者123 更新时间:2023-12-01 07:52:22 25 4
gpt4 key购买 nike

在尝试从 Controller 返回简单的 JSON 对象时,我遇到了一个奇怪的问题。在 Firefox 调试工具中,我可以很好地看到返回的 JSON;但是,在我的 javascript 回调中,我的 console.log(data)

中出现 undefined

帖子(查看)

 @{
string postAction = string.IsNullOrEmpty(Model._id) ? "AddCredential" : "EditCredential";
string title = string.IsNullOrEmpty(Model._id) ? "Add" : "Edit";
string onSuccessAction = "";

if (!string.IsNullOrEmpty(Model.CallerLccation))
{
onSuccessAction = "Credentials.finishAddingCredFromEditPage();";
}
else
{
onSuccessAction = "General.init();$.fancybox.close();";
}
}
<h3>@title Credential</h3>


@using (Ajax.BeginForm(postAction, new AjaxOptions { HttpMethod = "Post", OnSuccess = onSuccessAction, UpdateTargetId = "credentialList", InsertionMode = InsertionMode.Replace }))
{

Controller (这里没有错误)

 public ActionResult AddCredential(CredentialViewModel viewModel)
{

//.......................
if (!string.IsNullOrEmpty(viewModel.CallerLccation))
{
//location the new credential being edited from is different than the normal location _CredentialList.cshtml

return Json(new
{
callerLocation = viewModel.CallerLccation,
credentialsDict = (object)JsonConvert.SerializeObject(GetCredentials(), Formatting.None),
newName = viewModel.Name,
newKey = newDoc["id"]

});
}




return PartialView("_CredentialList", GetCredentialListViewModel());
}

回调

finishAddingCredFromEditPage: function (data) {
//diff logic for diff places add credential is called from

//UNDEFINED HERE
console.log(data);
switch(data.callerLocation) {
case "edit":
//reload phone dict
allCredentials = data.credentialsDict;

//add option to all dropdowns on the edit page
$('.credentialDropdown').each(function(i) {
$(this).prepend("<option selected></option>")
.attr("value", data.newKey)
.text(data.newName);
});



//reset stuff
General.init();
$.fancybox.close();
break;
}
},

我尝试过的

  1. JsonRequestBehavior.AllowGet 添加到我的 return Json() 语句
  2. 添加一个带有 id = credentialList 的虚拟 div,看看它是否会填充 JSON
  3. 删除了 UpdateTargetIdInsertionMode 属性

我认为,也许在幕后,MVC 正在发送期望 HTML 的请求,因为存在 updateTargetId,但这并不是问题所在。我检查了服务器的响应,JSON 确实存在。我不太确定还需要采取哪些其他步骤来解决此问题。

最佳答案

我明白了。显然,您不能在带有参数的回调上使用括号。不要引用我的观点,因为我没有找到任何相关文档,但根据我的观察,情况确实如此。我的看法改变了

onSuccessAction = "Credentials.finishAddingCredFromEditPage();";  

onSuccessAction = "Credentials.finishAddingCredFromEditPage";  

终于能够在回调中得到一些东西

关于jquery - Ajax.BeginForm 返回未定义的 JSON(开发工具显示正在返回 JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242848/

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