Javascript AJAX调用中返回的JSON无效
我试图通过向AJAX调用发送新插入的ID和JSON变量来检测数据库条目是否已成功输入,但它在phonegAP中不起作用,但在所有浏览器中都可以,我可以看到数据已成功插入数据库。感谢所有评论/帮助,谢谢。 AJAX代码-Javascript AJAX调用中返回的JSON无效,javascript,jquery,ajax,json,cordova,Javascript,Jquery,Ajax,Json,Cordova,我试图通过向AJAX调用发送新插入的ID和JSON变量来检测数据库条目是否已成功输入,但它在phonegAP中不起作用,但在所有浏览器中都可以,我可以看到数据已成功插入数据库。感谢所有评论/帮助,谢谢。 AJAX代码- function InsertQnA() { $.ajax({ url: Domain + '/Result/Create', cache: false, typ
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);
});
}
发现代码存在两个问题: 1.
localStorage.getItem(“总计”,总计)
应为localStorage.getItem(“总计”)
2.数据类型:“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);
});
}
从我(在过去两天中)收集的信息来看,MVC ActionResult似乎并不容易为Web应用程序提供数据。我通过使用string方法复制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({
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);
});
$.ajax({
url:Domain+'/Result/CreateResult',,
cache:false,
键入:“POST”,
数据类型:“json”,
contentType:'application/json;charset=utf-8',
“Q3:“,,”Q3:““““,,”,”Q3““““““,,”,”Q3““““,,”,”Q3““,,”,”Q4“““,,”Q4“““““,,,”Q5““““““““““““““““““““““““““,,”Q7““““““““:””””””,,“Q8““““““““““,,”Q9““““““:””””””””““““““““““““““““;””””””””””””””””””””””、、、,”Q9:“““““““““““““““““““““““““““““““““““““““““““““““““““““,“尝试”:“'+QnANumAttempts+'”}”,
成功:功能(数据){
试一试{
如果(数据!=0){
//用于反馈插入>更新结果实体的结果id
setItem(“ResultId”,data.ResultId);
UserId(“您已成功完成案例研究”+localStorage.getItem(“案例研究”)+”,请填写评估表。”);
//重置局部变量
ResetLocalStorage();
//计算用户的实体数
CountUserEntitiesInResults();
}
其他的
{
UserId(“您的条目尚未保存,请重试。”);
}
}捕获(错误){
警报(“这是一个错误,可能是:“+error.message”);
}
},
}).失败(
函数(xhr、textStatus、err){
console.log(xhr.statusText);
console.log(textStatus);
控制台日志(err);
});
您可能需要检查“数据”“从服务器返回。在iOS中,您可以轻松地将safari连接到iPhone模拟器。。对于android,您可以将console.log与eclipseAmitesh一起使用,我正在iPad上进行测试,我将如何进行测试?您可以在调试时从xcode中选择iPhone/iPad模拟器。应用程序启动后,您可以打开safari-->开发-->iPhone/iPad模拟器。之后,它将与safari中的调试相同。有关详细信息和实际设备调试,您可以访问,这有点帮助,谢谢。嗨,Surajit,我尝试添加数据类型:“json”,但这破坏了整个功能,即我没有收到任何警报。另一件事是+error.message。也破坏了函数。我已经编辑了上面的代码<代码>错误。消息是正确的。前一条在错误的末尾有一个点(.)。消息
谢谢。如果(data.Success==true)只有警报(“此警报已成功调用”),则现在不会触发此行;和警报(“此警报也未被调用”);我不确定我是否应该将ActionResult更改为返回内容而不是JSon?添加此行alert(data)代码>在警报之后(“此警报已成功调用”)代码>查看您得到了什么。似乎没有从您从ajax调用的url接收到任何数据。我已通过一些服务器端更改编辑了我的答案。
[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({
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);
});