javascript - JSON returned in AJAX call not working -
javascript - JSON returned in AJAX call not working -
i trying observe wither database entry has been input sending new inserted id , json variable ajax phone call not working in phonegap, fine in browsers , can see info beingness inserted in db successfully. comments/ help appreciated, thanks. ajax code -
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 invoked successfully'); if (data.success == true) { alert('this alert not beingness invoked successfully'); //result id used feedback insertion > update result entity localstorage.setitem("resultid", data.resultid); viewmodel.userid("you have completed case study " + localstorage.getitem("casestudy") + ", please fill out <a href='evaluation.html' target='_self'>evaluation.</a>"); } else if (data.success==false) { alert('this alert not beingness invoked either'); viewmodel.userid("your entry has not been saved, please seek again."); } }, }).fail( function (xhr, textstatus, err) { console.log(xhr.statustext); console.log(textstatus); console.log(err); }); }
mvc function
// // 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; seek { //valid database entry..send new resultid homecoming json(new { success = true, resultid, jsonrequestbehavior.allowget }); } grab { // no database entry homecoming json(new { success = false, message = "error", jsonrequestbehavior.allowget }); } } homecoming redirecttoaction("index"); } homecoming view(result); }
found 2 issues code:
1.localstorage.getitem("total", total)
should localstorage.getitem("total")
2.datatype : "json"
not explicitly mentioned.
i have posted relevant corrections.try if helps , share if if errors.
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 invoked successfully'); seek { if (data.success == true) { alert('this alert not beingness invoked successfully'); //result id used feedback insertion > update result entity localstorage.setitem("resultid", data.resultid); viewmodel.userid("you have completed case study " + localstorage.getitem("casestudy") + ", please fill out <a href='evaluation.html' target='_self'>evaluation.</a>"); } else if (data.success==false) { alert('this alert not beingness invoked either'); viewmodel.userid("your entry has not been saved, please seek again."); } }catch(error) { alert("this error might be: "+error.message); } }, }).fail( function (xhr, textstatus, err) { console.log(xhr.statustext); console.log(textstatus); console.log(err); }); }
make these changes in server side code.
var json = ""; seek { //valid database entry..send new resultid json = json.encode(new { success = true, resultid, jsonrequestbehavior.allowget }); } grab { // no database entry json = json.encode(new { success = false, message = "error", jsonrequestbehavior.allowget }); } response.write(json);
javascript jquery ajax json cordova
Comments
Post a Comment