簡體   English   中英

從mvc函數返回的AJAX調用數據未定義

[英]AJAX call data returned from mvc function is undefined

我知道這已經被問過1000次了,但是我遇到了麻煩。^已經創建了一個Web應用程序,可以為用戶插入用戶數據和反饋,並且下面的代碼基本上是PhoneGap應用程序的一部分。 奇怪的是,該代碼可以在Web瀏覽器中完美運行,但不能在Phonegap(通過Xcode輸出iPad)中完美運行。

因此有人會知道為什么在成功回調和alert(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);
                         });​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM