c# - JSON request returns object error using JQUERY -
i have aspx page returns valid json - when called via jquery can see in fiddler json returned error thrown [object error].
protected void page_load(object sender, eventargs e) { string json = "{\"name\":\"joe\"}"; response.clearheaders(); response.clearcontent(); response.clear(); response.cache.setcacheability(httpcacheability.nocache); response.contenttype = "application/json"; response.contentencoding = encoding.utf8; response.write(json); response.end(); } the html page consuming page in different domain, , using jsonp.
function jsonpcallback(response){ alert(response.data); } $(document).ready(function(){ $.ajax({ url: 'http://localhost:30413/getprice.aspx', datatype: 'jsonp', error: function(xhr, status, error) { alert(error); }, success: jsonpcallback }); }); when aspx page requested valid json returned browser, when jquery call made json returned callback function not called , expected ";" js error [object error] message displayed. below request , responses made.
i've tried every variation of request same result. using request jquery sample below because works in last sample shown below.
get http://localhost:30413/price.aspx?callback=jquery17105556924406763212_1338876162569&_=1338876162581 http/1.1 accept: application/javascript, */*;q=0.8 referer: http://alpha.tigerdirect.com/applications/b2b/varinfo.asp accept-language: en-us user-agent: mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; wow64; trident/5.0) accept-encoding: gzip, deflate host: localhost:30413 connection: keep-alive pragma: no-cache cookie: asp.net_sessionid=d4pje2hgm2beznslfpp4pii5 http/1.1 200 ok server: asp.net development server/10.0.0.0 date: tue, 05 jun 2012 06:02:42 gmt x-aspnet-version: 4.0.30319 cache-control: no-cache pragma: no-cache expires: -1 content-type: application/json; charset=utf-8 content-length: 14 connection: close {"name":"joe"} this sample works
function jsonpcallback(response){ alert(response.data); } $(document).ready(function(){ $.ajax({ url: 'http://api.bitbucket.org/1.0/repositories/retroviz/webformsthemeswitcher/src/tip/.hgignore', datatype: 'jsonp', error: function(xhr, status, error) { alert(error); }, success: jsonpcallback }); });
change url in ajax url: http://localhost:30413/getprice.aspx?callback=?
and
if(request.querystring['callback']!=null){ string callback = request.querystring['callback']; string json = "{\"name\":\"joe\"}"; response.clearheaders(); response.clearcontent(); response.clear(); response.cache.setcacheability(httpcacheability.nocache); response.contenttype = "application/json"; response.contentencoding = encoding.utf8; response.write(callback + "(" + json + ")"); response.end(); } in jsop, wrap response in callback function call. call function in javascript.
Comments
Post a Comment