jquery - MVC 3 JSON call not working in IIS -
i'm working in mvc 3 application aspx engine , point of start developed simple search utilizes jquery json call retrieve info. call sends parameter taken text input , updates table results. funcion this:
function performlookup() { var _accountnumber = $('#accountnumber').val(); $.ajax({ url: '/searchajax/searchaccount', type: 'post', data: '{_accountnumber:'+_accountnumber+'}', datatype: 'json', contenttype: 'application/json; charset=utf-8', success: function (data) { updatetable(data); }, error: function () { alert('an error occurred while performing search.'); } }); return false; }
the server code runs query parameter , returns list serialized json worked jquery. server code looks this:
[httppost] public jsonresult searchaccount(string _accountnumber) { mlibentities dbmlib = new mlibentities(); var searchresults = (from s in dbmlib.sets s.setmap1 == _accountnumber select s); return json(searchresults.tolist()); }
as see it's nothing difficult , works when run project vs2010 , use virtual machine.
the problem happens when publish project in windows 2008 server iis 7. project runs when run performlookup function message "an error occurred while performing search" meaning ajax call failed.
does have idea why call failing in iis while working in vs2010 virtual machine? missing configuration iis wise?
thanks in advance!
never hardcode urls because when deploy application there virtual directory prepended urls:
url: '/searchajax/searchaccount',
always use url helpers when dealing urls:
url: '<%= url.action("searchaccount", "searchajax") %>',
so here's how refactor code:
function performlookup() { var _accountnumber = $('#accountnumber').val(); $.ajax({ url: '<%= url.action("searchaccount", "searchajax") %>', type: 'post', data: json.stringify({ _accountnumber: _accountnumber }), datatype: 'json', contenttype: 'application/json; charset=utf-8', success: function (data) { updatetable(data); }, error: function () { alert('an error occurred while performing search.'); } }); return false; }
or if performlookup
function called when link clicked, have link generated html helper:
<%= html.actionlink( "perform search", "searchaccount", "searchajax", null, new { id = "search" } ) %>
and ajaxify it:
$(function() { $('#search').click(function() { var _accountnumber = $('#accountnumber').val(); $.ajax({ url: this.href, type: 'post', // no need send json request // i've replaced standard // application/x-www-form-urlencoded post request data: { _accountnumber: _accountnumber }, datatype: 'json', success: function (data) { updatetable(data); }, error: function () { alert('an error occurred while performing search.'); } }); return false; }); });
and recommend using firebug excellent tool allowing debug kind of problems shows ajax requests , what's happening between client , server.
Comments
Post a Comment