mvc函数返回的AJAX调用数据未定义

AJAX call data returned from mvc function is undefined

本文关键字:调用 数据 未定义 AJAX 函数 返回 mvc      更新时间:2023-09-26

我知道这个问题以前已经被问过1000次了,但我用这个问题碰壁了^已经创建了一个web应用程序,它为用户插入用户数据和反馈,下面的代码基本上是PhoneGap应用程序的一部分。奇怪的是,该代码在网络浏览器中运行得很好,但在Phonegap(通过Xcode输出iPad)中却不能。

因此,如果有人知道为什么在成功回调和警报(data.ResultId)之后,我在下面的AJAX调用中收到了一个未定义的错误,我们将不胜感激。非常感谢。

// 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 });
            }
        }

AJAX

$.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);
                         });​