Relative Path Problems in Javascript Ajax call -
okay, have javascript file following functions:
function askreason() { var answer = prompt("please enter reason action:", ""); if (answer != null) doreason(answer); } function createxmlhttprequest() { try { return new xmlhttprequest(); } catch (e) { alert('xmlhttprequest not working'); } try { return new activexobject("msxml2.xmlhttp"); } catch (e) { alert('msxml2.xmlhtt not working'); } try { return new activexobject("microsoft.xmlhttp"); } catch (e) { alert('microsoft.xmlhttp not working'); } alert("xmlhttprequest not supported"); return null; } function doreason(reason) { var xmlhttpreq = createxmlhttprequest(); var url = "/shared/askreason.ashx?reason=" + reason; xmlhttpreq.open("get", url, true); xmlhttpreq.send(null); }
this line:
var url = "/shared/askreason.ashx?reason=" + reason;
is causing problem.
in vs 2010 debugging app - call works ashx handler.
when move project virtual directory - example http://localhost/myapp
this code break , have change javascript this:
var url = "http://localhost/myapp/shared/askreason.ashx?reason=" + reason;
any ideas on how can fix work in both environments or accept manual change when deploy apps servers?
thanks, mike
pointy's way works, have know in advance you're going deploy it.
alternately, don't start relative path /
:
var url = "shared/askreason.ashx?reason=" + reason;
that resolved relative current document's location. if current document is:
http://localhost/myapp/index.aspx
...then resolve to
http://localhost/myapp/shared/askreason.ashx?reason=foo
Comments
Post a Comment