我有2个基本的测试应用程序,我做Ajax请求。在调用ajax请求后,对于第一个请求,我需要在检索值之前解析ajax data.responseText。对于第二个,我可以直接使用访问值。为什么有时我必须parseJSON,有时候不需要做Ajax请求?
代码的第一个应用程序:
@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);
}
对于这一个,我直接中科院访问值。
UPDATE
这里是我的控制器的操作方法:
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 });
}
为什么?
谢谢。
“@ using”语法是什么? Ajax对象来自哪里? – 2012-02-17 17:08:10
这是来自ASP.NET MVC语法。 Ajax对象来自那里。 – Bronzato 2012-02-17 17:15:04
return Json(新的 { 成功= true, 消息=“该人已被添加!” }); – Bronzato 2012-02-17 17:15:44