jquery - Why is my AJAXoned Action's return not being seen as successful by the caller? -


in asp.net mvc app, i've got ajax call in view's script section:

$(".ckbx").change(function () { . . .     $.ajax({         type: 'get',         url: '@url.action("getunitreportpairvals", "home")',         data: { unit: unitval, report: rptval },         cache: false,         success: function (result) {             alert(result);         }     }); }); 

stepping through controller action being called:

public actionresult getunitreportpairvals(string unit, string report) {     homemodel model = new homemodel();      int rptid = getreportidforname(report);      datatable unitreportpairemailvalsdt = new datatable();     unitreportpairemailvalsdt = sql.executesqlreturndatatable(         sql.unitreportpairemailquery,          commandtype.text,          new sqlparameter()                 {                     parametername = "@unit",                     sqldbtype = sqldbtype.varchar,                     value = unit                 },                 new sqlparameter()                 {                     parametername = "@rptid",                     sqldbtype = sqldbtype.int,                     value = rptid                 }                 );      model.unitreportpairemailvals = unitreportpairemailvalsdt;     return view(model); } 

...it seems working fine; "model" contains expected value in unitreportpairemailvals.

but never see alert message here in ajax call:

success: function (result) {     alert(result); }  

so why "success" function of jquery ajax call not being reached?

update

btw, may clue: final line of controller method:

return view(model); 

...paints word "view" red rhode island rooster's comb. if it's not supposed that, supposed be? that's exact same thing have @ end of "public actionresult index()" controller action, works fine.

update 2

i changed end of ajax call to:

success: function (result) {     alert(result); }, failure: function (error) {     alert(error); } 

...and end of controller method to:

return json(new { result = model }, jsonrequestbehavior.allowget); 

...but no alert, neither success nor failure.

update 3

note: when tried this:

var model = json.stringify({ unit: unitval, report: rptval });     $.ajax({         type: 'get',         url: '@url.action("getunitreportpairvals", "home")',         data: model,         contenttype: 'application/json',         cache: false,         success: function (result) {             alert(result);         },         error: function (result) {             debugger;         }     }); 

..."unit" null, , wouldn't fly @ all.

when changed (removed line 1 , changed "data" arg had been using previously):

$.ajax({     type: 'get',     url: '@url.action("getunitreportpairvals", "home")',     data: { unit: unitval, report: rptval },      contenttype: 'application/json',     cache: false,     success: function (result) {         alert(result);     },     error: function (result) {         alert('failed');     } }); 

..."unit" expect (and "report", too), see 'failed'.

update 4

once pieced several different answers , related questions, wrote tip on how here.

update 5

i've got [working] ajax call [working]:

var model = json.stringify({ unit: unitval, report: 1 }); $.ajax({     type: 'get',     url: '@url.action("getunitdatarangeparamsvals", "unitreportdatarangeparams")',     data: { unit: unitval, report: 1 },     contenttype: 'application/json',     cache: false,     success: function (returneddata) {         populatedatarangeprams(1, returneddata);     },     error: function () {         alert('error - returneddata.error.stringify');     } }); 

...but i'm not using "model" that's declared and, according understanding, paired "contenttype: 'application/json',"

so thought should instead:

var model = json.stringify({ unit: unitval, report: 1 }); $.ajax({     type: 'get',     url: '@url.action("getunitdatarangeparamsvals", "unitreportdatarangeparams")',     data: model,     contenttype: 'application/json',     cache: false,     success: function (returneddata) {         populatedatarangeprams(1, returneddata);     },     error: function () {         alert('error - returneddata.error.stringify');     } }); 

now data passed controller's method in json stringified format (encapsulated in "model").

but, doesn't work.

it works first block, not second.

so removed both "model" , "contenttype" lines, , changed "data" line had been, , works fine. useless, or employing them wrongly?

here few snippets use when doing ajax in asp.net mvc.

$(".ckbx").change(function () {     .....     var model = json.stringify({ unit: unitval, report: rptval });     $.ajax({         type: 'get',         url: '@url.action("getunitreportpairvals", "home")',         data: model,         contenttype: 'application/json',         cache: false,         success: function (result) {             alert(result);         },         error: function(result) {             debugger;         }     }); }); 

sometimes 2 changes (as compared original) not necessary asp.net can picky if json isn't right , helps that. since able step controller method, wasn't problem, can handy.

your real problem return type on controller method. actionresult means going corresponding view , build out html based on view. if doing ajax don't want that.

public jsonresult getunitreportpairvals(string unit, string report) {     // stuff     // create response model     var model = ...//whatever data returning here     return json(model); } 

this "standard" approach ajax calls in asp.net mvc.

as noted in comment, actionresult works great in index method. yes, because purpose of index method load page. generally, if method used load page you'll use actionresult because use view create html page. if submitting info , returning response "success" don't want whole html response, want json. that's why jsonresult works here.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -