asp.net - MVC 5 JsonResult returns html? -
i have been following tutorial https://www.youtube.com/watch?v=c_melpfxjug regarding ajax , jsonresult in homecontroller
i did tutorial, reason controller returning html , not json
i did not change 1 line of code, it's failing parseerror on javascript side.
when @ response see html page, not json object.
controller code:
public jsonresult doublevalue(int? value) { if (!request.isajaxrequest() || !value.hasvalue) { return null; } else { int doublevalue = value.value * 2; var ret = new jsonresult { data = new { doublevalue = doublevalue } }; return ret; } } cshtml:
@using (html.beginform()) { @html.textbox("txtamount",0) <button id="btndoublevalue">doubleit</button> <div id="lblmessage"></div> } @section scripts{ <script type="text/javascript"> $(function () { $('#btndoublevalue').on('click', function() { $.ajax({ type: 'post', url: '@html.action("doublevalue")', data: { 'value': $('#txtamount').val() }, datatype: 'json', cache: 'false' }).success(function (data) { var t = data; $('#txtamount').val(data.doublevalue); }).error(function (x, o, e) { $('#lblmessage').html('error found: ' ); }); return false; }) }); </script> }
found error using html.action , not url.action -> human error suppose
from reference:
html.action - returns result html string. it works now
$.ajax({ type: 'post', url: '@url.action("doublevalue")', //<--- url.action data: { 'value': $('#txtamount').val() }, datatype: 'json', cache: 'false'
Comments
Post a Comment