c# - Cannot get response from WebAPI -
update : have better understanding of error. facing error explained here : https://stackoverflow.com/a/20035319/3021830. should fix.
i have rest api created asp.net webapi. hosted on http://localhost:54700/. have method route "api/refdata", need call http://localhost:54700/api/refdata. marked allowanonymous
attribute
when make call postman seems work fine. try call asp.net mvc application, both server , client can not result.
my server side code :
private static httpclient _client; private static httpclient client { { if (_client == null) { _client = new httpclient(); _client.baseaddress = new uri("http://localhost:54700/"); _client.defaultrequestheaders.accept.clear(); _client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); } return _client; } } internal list<heardabout> heardabouts { { if (system.web.httpcontext.current.session["refdata"] == null) { getrefdata().wait(); } return system.web.httpcontext.current.session["refdata"] list<refdata>; } } private async task<list<refdata>> getrefdata() { try { list<refdata> has = new list<refdata>(); // following line code leaves debugging httpresponsemessage response = await client.getasync("api/refdata"); if (response.issuccessstatuscode) { has = await response.content.readasasync<list<refdata>>(); system.web.httpcontext.current.session["refdata"] = has; } return has; } catch (exception ex) { throw; } }
and client side code is:
$.ajax({ type: "get", url: "http://localhost:54700/api/refdata", cache: false, contenttype: "application/json; charset=utf-8", success: function (response) { if (callback) callback(response.d); }, error: function (response) { if (callback) error(response.d); }, });
the server-side codes reaches commented line. leaves debugging. wrapped code try-catch
block not raise exception well.
the client-side code falls error statustext "error".
what wrong these codes , how may fix?
thanks in advance.
as have denoted in update calls failed because domain addresses of web application , webapi different.
to solve problem have used information provided here : enabling cross-origin requests in asp.net web api 2. , works.
Comments
Post a Comment