i have verified using browser debugger / network monitor sending in variable in ajax call called "search" , in monitor shows in header "search=test". trying access variable in code behind can't seem find it? here code front , back-end, missing something?
code behind :
public class ajaxsearchlog : ihttphandler { public void processrequest (httpcontext context) { context.response.contenttype = "text/plain"; string searchterm = convert.tostring(httpcontext.current.request.form["search"]); if(!string.isnullorempty(searchterm)){ aspdotnetstorefrontcore.db.executesql("insert tribsearch(searchterm,customerid,localesetting) values(" + db.squote(commonlogic.ellipses(searchterm, 97, true)) + "," + '0' + "," + db.squote("en-us") + ")"); } context.response.clear(); context.response.write(searchterm); context.response.end(); } public bool isreusable { { return false; } } }
front-end
window.onbeforeunload = function(e) { if ($("#searchbox_text").val().length >= 2) { var _search = $("#searchbox_text").val(); var url = "/ajaxsearchlog.ashx"; $.ajax({ async: false, type: 'post', url: url, data: {'search': _search}, contenttype: 'application/json;', datatype: 'json', success: function(result) { alert(result); }, error: function(xhr, textstatus, errorthrown) { console.log('error'); } }); } };
i have found data wasn't getting passed through when contenttype
set 'application/json'. also, read post variable, request.params[]
needed. here 2 lines of code changed :
in front-end - contenttype: 'application/x-www-form-urlencoded; charset=utf-8',
in back-end - string searchterm = convert.tostring(context.request.params["search"]);