In Visual Studio, I have some Javascript code on a site I'm developing. While I'm debugging I'm using the $ajax call to "localhost". When deployed, it will need to be the actual server:
$('#textInput_UserName').focusout(function () {
var _username = $('#textInput_UserName').val();
$.ajax({
url: 'http://localhost:8809/Account/UserNameExists/',
data: { username: _username },
dataType: 'html',
});
When I publish, I need to transform that localhost to the actual domain:
$('#textInput_UserName').focusout(function () {
var _username = $('#textInput_UserName').val();
$.ajax({
url: 'http://www.mydomain.com/Account/UserNameExists/',
data: { username: _username },
dataType: 'html',
});
Is there an easy/automatic way to do this, similar to the way Web Config transforms work?
Many thanks!
You don't, you just omit the host, the browser will fill this in for you, like this:
$('#textInput_UserName').focusout(function () {
var _username = $('#textInput_UserName').val();
$.ajax({
url: '/Account/UserNameExists/',
data: { username: _username },
dataType: 'html',
});
If you're actually talking about x-domain requests, which I doubt you are, then just set a global js site variable.
I recommend you to use this:
url: '<%= ResolveClientUrl("~/Account/UserNameExists/")',
If you do it this way you'll avoid problems if you:
You can also expose a public property in your page/user control/master page, and use it from code in the same way, i.e:
public string ServiceUrl { get { return ResolveClientUrl("~/Account/UserNameExists/");}
url: '<%= ServiceUrl',
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With