为什么执行不了ajax,为什么有时我不得不parseJSON,有时不执行ajax请求?

我有两个基本的测试应用程序,我在那里执行ajax请求。在调用ajax请求之后,对于第一个请求,我需要在检索值之前解析ajax data.responseText。对于第二个,我可以直接访问值。

第一个应用的代码:

@using (Ajax.BeginForm("JsonAdd", "People", new AjaxOptions { OnComplete = "JsonAdd_OnComplete" }))

{

...

}

function JsonAdd_OnComplete(data) {

var json = $.parseJSON(data.responseText);

if (json.Success) {

$("#PersonList").html(json.PartialViewHtml);

}

$("#addPersonModal").slideToggle();

$("#message").html(json.Message);

}

如您所见,我需要在获取值之前先解析data.responseText。

这是第二个应用程序:

@using (Ajax.BeginForm("_NewPersonToKeepInformed", "General", new { id = "NewPersonToKeepInformed" }, new AjaxOptions { Confirm = "Are you sure ?", HttpMethod = "POST", OnSuccess = "AddedSuccess" }))

function AddedSuccess(response) {

alert(response.message);

}

对于这个,我直接访问值。

更新

这是我的控制器操作方法:

public JsonResult JsonAdd(AddPersonViewModel AddPersonModel)

{

...

return Json(new

{

Success = true,

Message = "The person has been added!"

});

}

这是第二个:

public JsonResult _NewPersonToKeepInformed(NewPersonToKeepInformedViewModel viewModel)

{

...

// return newly added person in a Json object

return Json(new { message = "Ajout effectué", firstName = viewModel.FirstName, lastName = viewModel.LastName, phone = viewModel.Phone, mail = viewModel.Mail });

}

为什么?

感谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值