Javascript mvc函数返回的AJAX调用数据未定义
我知道这个问题以前被问过1000次,但我遇到了麻烦。^我创建了一个web应用程序,可以插入用户数据和用户反馈,下面的代码基本上是PhoneGap应用程序的一部分。奇怪的是,这段代码在网络浏览器中运行良好,但在通过Xcode的Phonegap输出iPad中却无法运行 因此,有人知道为什么在成功回调和alertdata.ResultId之后,下面的AJAX调用会出现未定义的错误吗,感谢您的帮助。 谢谢大家!Javascript mvc函数返回的AJAX调用数据未定义,javascript,jquery,ajax,cordova,Javascript,Jquery,Ajax,Cordova,我知道这个问题以前被问过1000次,但我遇到了麻烦。^我创建了一个web应用程序,可以插入用户数据和用户反馈,下面的代码基本上是PhoneGap应用程序的一部分。奇怪的是,这段代码在网络浏览器中运行良好,但在通过Xcode的Phonegap输出iPad中却无法运行 因此,有人知道为什么在成功回调和alertdata.ResultId之后,下面的AJAX调用会出现未定义的错误吗,感谢您的帮助。 谢谢大家! // POST: /Result/Create [HttpPost] public Acti
// POST: /Result/Create
[HttpPost]
public ActionResult Create(Result result)
{
if (ModelState.IsValid)
{
result.ResultDate = DateTime.Now;
repository.InsertResult(result);
repository.Save();
if (Request.IsAjaxRequest())
{
int ResultId = result.ResultId;
try
{ //valid database entry..send back new ResultId
return Json(new { Success = true, ResultId, JsonRequestBehavior.AllowGet });
}
catch
{ // no database entry
return Json(new { Success = false, Message = "Error", JsonRequestBehavior.AllowGet });
}
}
return RedirectToAction("Index");
}
return View(result);
}
插入QnA
function InsertQnA() {
//hardcoded for testing
Q1 = 10;
Q2 = 10;
Q3 = 10;
Q4 = 10;
Q5 = 10;
Q6 = 10;
Q7 = 10;
Q8 = 10;
Q9 = 10;
Q10 = 10;
localStorage.setItem("Total",100);
localStorage.setItem("CaseStudy", 1);
localStorage.setItem("UserId",1);
Attempts = "1";
////////////////
$.ajax({
url: Domain + '/Result/Create',
cache: false,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{"Q1":"' + Q1 + '","Q2":"' + Q2 + '","Q3":"' + Q3 + '","Q4":"' + Q4 + '","Q5":"' + Q5 + '","Q6":"' + Q6 + '","Q7":"' + Q7 + '","Q8":"' + Q8 + '","Q9":"' + Q9 + '","Q10":"' + Q10 + '","Total":"' + localStorage.getItem("Total") + '","CaseStudy":"' + localStorage.getItem("CaseStudy") + '","UserId":"' + localStorage.getItem("UserId") + '","Attempts":"' + QnANumAttempts + '"}',
// dataType : "json",
success: function (data) {
alert(data.ResultId);
if (data.Success==true) {
}
else if (data.Success==false) {
viewModel.UserId("Your entry has not been saved, please try again.");
}
},
}).fail(
function (xhr, textStatus, err) {
console.log(xhr.statusText);
console.log(textStatus);
console.log(err);
});
}
问题是,我打算使用相同的ActionResult来提供MVC视图和htlm5 cordova iOS应用程序。我通过复制ActionResult来解决这个问题,但是将返回类型更改为字符串。注意,代码在操作中看起来有点不同,但是最初的操作也很好。非常感谢所有发帖的人
[HttpPost]
public string CreateResult(Result result)
{
result.ResultDate = DateTime.Now;
repository.InsertResult(result);
repository.Save();
if (result == null)
{
// User entity does not exist in db, return 0
return JsonConvert.SerializeObject(0);
}
else
{
// Success return user
return JsonConvert.SerializeObject(result, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
}
}
阿贾克斯
我刚看到你的if语句没有主体。这可能是造成问题的原因吗?如果你指的是html正文,为了简洁,我去掉了代码。不,我指的是两个花括号之间的代码,如ifstation{doSomething}是的,为了简洁,我也去掉了它,问题在于返回的数据是未定义的,即alertdata.ResultId,或者如果我使用alertdata.Success,它们都是未定义的?这听起来更像是服务器端的问题,因为ajax调用get是一个答案,否则它不会调用Success方法,但您的数据没有设置
$.ajax({
url: Domain + '/Result/CreateResult',
cache: false,
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: '{"Q1":"' + Q1 + '","Q2":"' + Q2 + '","Q3":"' + Q3 + '","Q4":"' + Q4 + '","Q5":"' + Q5 + '","Q6":"' + Q6 + '","Q7":"' + Q7 + '","Q8":"' + Q8 + '","Q9":"' + Q9 + '","Q10":"' + Q10 + '","Total":"' + localStorage.getItem("Total") + '","CaseStudy":"' + localStorage.getItem("CaseStudy") + '","UserId":"' + localStorage.getItem("UserId") + '","Attempts":"' + QnANumAttempts + '"}',
success: function (data) {
try {
if (data != 0) {
//result id used for feedback insertion > update result entity
localStorage.setItem("ResultId", data.ResultId);
viewModel.UserId("You have successfully completed case study " + localStorage.getItem("CaseStudy") + ", please fill out the <a href=evaluation.html target=_self>evaluation.<a/>");
//reset locals
ResetLocalStorage();
//count number of entities for User
CountUserEntitiesInResults();
}
else
{
viewModel.UserId("Your entry has not been saved, please try again.");
}
}catch(error) {
alert("This is the error which might be: "+error.message);
}
},
}).fail(
function (xhr, textStatus, err) {
console.log(xhr.statusText);
console.log(textStatus);
console.log(err);
});