我想通過發送新插入的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);
}
你可能要檢查的 「數據」 從服務器返回。在iOS中,您可以輕鬆地將Safari添加到iPhone模擬器。對於android,您可以使用console.log和eclipse – Amitesh 2014-10-27 12:13:54
Amitesh,我正在iPad上測試此操作,我將如何處理這個問題? – 2014-10-27 15:58:40
您可以在調試時從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