javascript - ASP.NET MVC/jQuery/AJAX: link fails to work first time, but works second time (after page reloads) -
preface
for question, have mvc partial view. view has section displays list of documents. each document has hyperlink: when clicked, hyperlink takes user second page view displaying additional information.
the link inside unordered list:
<a style="text-decoration:underline;" onclick="sendtodocketsearch('@currentdocument.dktyear','@currentdocument.dktsequence','@currentdocument.dktsubactionid');">@currentdocument.dktyear.tostring().padleft(2, '0') - @currentdocument.dktsequence.tostring().padleft(5, '0')</a> when user clicks link, takes them sendtodocketsearch javascript function (to prepare search document):
var sendtodocketsearch = function (yearofdocket, sequenceofdocket, dktsubactionidofdocket) { jquery.ajax({ type: "post", url: "@url.action("docketsearchondemand")", datatype: "json", contenttype: "application/json; charset=utf-8", data: json.stringify({ docketyear: yearofdocket, docketsequence: sequenceofdocket, dktsubactionid: dktsubactionidofdocket, userisauthorized: '@model.userisauthorized' }), success: function (data) { alert(data); }, failure: function (errmsg) { alert(errmsg); } }); submitform(); } note page/view/form submitted after following controller method run:
public actionresult docketsearchondemand(string docketyear, string docketsequence, decimal dktsubactionid, bool userisauthorized, portalindexview viewmodel) { system.web.httpcontext.current.session.add("userisauthorized", userisauthorized); string docketsearch = docketyear + "-" + docketsequence; system.web.httpcontext.current.session["docketsearchondemand"] = docketsearch; if (dktsubactionid > 0) { system.web.httpcontext.current.session["dktsubactionid"] = dktsubactionid.tostring(); system.web.httpcontext.current.session["searchingcustomid"] = true; } else { system.web.httpcontext.current.session["dktsubactionid"] = "1"; system.web.httpcontext.current.session["searchingcustomid"] = false; } return view(viewmodel); } the above controller method runs; then, because form submitted, httppost action page takes place. when running on local pc, link clicked , next page loaded without drama.
problem
the problems start when upload code dev/test server. don't know how use breakpoints while troubleshooting active website, follow along browser developer tool monitor network traffic.
when clicking link when running website on localserver, process continues:
- the hyperlink takes me method pass information searched
- the page/view/form submitted
- the controller redirects have go.
when click link on site and it's on server, first click ignored - network traffic shows tries navigate controller via javascript function above, failure happens fast can't take screenshot of it. page reloads second time @ point.
when click on same link second time, works without fail.
i believe view/javascript/controller code works because works second time (and on subsequent attempts). flagrantly fails first time on server; after that, user fine. i'd prevent "first-time" failure, however, , i'm wondering problem be...
- bad timing
i may passing information (or late website/server process properly). page correctly second time, maybe i'm "jumping gun" not waiting little longer page-loading processes sort out. (maybe can fiddle around $(document).ready() javascript portion of first page "delay" allowing people click link.)
- code error
i'll glad admit bad code if i'm genuinely messing up. maybe it's javascript function, or maybe it's code in controller; @ rate, making first pass of function call rejected. maybe code bad because problem doesn't happen second time, , i'm getting false sense of security (i.e. there problems code system willing forgive after page has thoroughly loaded).
- server problem/miscellaneous
i'm wondering if missed when uploaded latest changes, or if should have contacted network team in case there permissions need activated site work smoothly. i'm in touch them regarding else, might take advantage of opportunity today.
there alternative in place me prevent problem happening, want find out why "first-time" failure happens. other similar actions fail first time on site, , i'd apply insights fixing issue them.
thank looking @ issue. have great day.
are sure want call submitform(); before jquery.ajax has finished? ajax call async hit submitform(); before has had time finish. should submitform(); in success event instead?
Comments
Post a Comment