2014-10-27 47 views
0

我想通過發送新插入的ID和JSON變量到AJAX調用來檢測數據庫條目是否已成功輸入,但它在phonegAP中不起作用,但它沒有問題所有的瀏覽器,我都可以看到數據正在成功插入數據庫。所有評論/幫助表示感謝,謝謝。 AJAX代碼 -在AJAX調用中返回的JSON不起作用

function InsertQnA() { 

      $.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", Total) + '","CaseStudy":"' + localStorage.getItem("CaseStudy") + '","UserId":"' + localStorage.getItem("UserId") + '","Attempts":"' + QnANumAttempts + '"}', 
       success: function (data) { 

       alert('this alert is invoked successfully'); 

        if (data.Success == true) { 

        alert('this alert is not being invoked successfully'); 

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

        } 
        else if (data.Success==false) 
        { 
       alert('this alert is not being invoked either'); 
         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); 
         }); 

     } 

MVC功能

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

     } 
+0

你可能要檢查的 「數據」 從服務器返回。在iOS中,您可以輕鬆地將Safari添加到iPhone模擬器。對於android,您可以使用console.log和eclipse – Amitesh 2014-10-27 12:13:54

+0

Amitesh,我正在iPad上測試此操作,我將如何處理這個問題? – 2014-10-27 15:58:40

+1

您可以在調試時從xcode選擇iPhone/iPad模擬器。一旦你的應用程序啓動,你可以打開Safari瀏覽器 - >開發 - > iPhone/iPad模擬器。之後它將與safari中的調試相同。 有關詳細信息和實際設備調試,您可以訪問http://webdesign.tutsplus.com/articles/quick-tip-using-web-inspector-to-debug-mobile-safari--webdesign-8787 – Amitesh 2014-10-28 05:42:57

回答

0

從我收集的(過去兩天)MVC ActionResult似乎並不容易爲數據提供webapp。我用一個字符串方法複製了ActionResult,並且現在它工作正常。可能不是最好的解決方案,但我聽說最好創建兩種操作方法,而不是在提供Web視圖和Web應用程序時使用兩種操作方法。非常感謝發佈的所有人。

MVC行動 -

[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); 
         });​ 
1

發現了兩個與代碼問題:

1. localStorage.getItem("Total", Total)應該localStorage.getItem("Total")

2. dataType : "json"沒有明確提及。

我已經發布了相關的更正。嘗試這個如果有幫助,也分享如果你有任何錯誤。

function InsertQnA() { 
    $.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('this alert is invoked successfully'); 

      try { 

       if (data.Success == true) { 

       alert('this alert is not being invoked successfully'); 

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

       } 
       else if (data.Success==false) 
       { 
      alert('this alert is not being invoked either'); 
        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); 
        }); 

    } 

在服務器端代碼中進行這些更改。

 var json = ""; 
     try 
      { //valid database entry..send back new ResultId 
        json = Json.Encode(new { Success = true, ResultId, JsonRequestBehavior.AllowGet }); 
      } 
       catch 
      { // no database entry 
        json = Json.Encode(new { Success = false, Message = "Error", JsonRequestBehavior.AllowGet }); 
      } 
     Response.Write(json); 
+0

嗨Surajit,我試着添加dataType:「json」,但這打破了整個功能,即我沒有得到任何警報工作。其他的事情是+ error.message。也打破了功能。 – 2014-10-28 11:02:47

+0

我編輯了上面的代碼。 'error.message'是正確的。 前一個在'error.message'結尾有一個點(。)。 – 2014-10-28 16:22:19

+0

謝謝。如果(data.Success == true)只有alert('this alert is invoked successfully'),那麼這行不會被觸發。並提醒('這個警報沒有被調用');林不知道如果我應該改變我的ActionResult返回內容而不是JSon? – 2014-10-28 17:12:08