asp.net mvc - AJAX POST to MVC Controller showing 302 error -
i want ajax post in mvc view. i've written following:
script code in view
$('#media-search').click(function () { var data = { key: $('#search-query').val() }; $.ajax({ type: 'post', url: '/builder/search', data: json.stringify(data), datatype: 'json', contenttype: 'application/json; charset=utf-8', success: function (data) { $('.builder').empty(); alert("key passed successfully!!!"); } }); });
controller code
[httppost] public actionresult search(string key) { return redirecttoaction("simple", new { key=key }); }
but on ajax post getting 302 found error
the '302' response code redirect. controller action explicitly returns redirecttoaction
, returns 302. since redirect instruction consumed ajax call , not directly browser, if want browser redirected, need following:
$.ajax({ type: 'post', url: '/builder/search', data: json.stringify(data), datatype: 'json', contenttype: 'application/json; charset=utf-8', success: function (data) { if (data.redirect) { window.location.href = data.redirect; } $('.builder').empty(); alert("key passed successfully!!!"); } });
if not, you'll need return more meaningful redirect instruction controller.
Comments
Post a Comment