I am working with an ASP MVC app. I created a clients.js file that has an ajax method that calls a controller and then retrieves the data. My problem is that the URL where that page sits looks something like this:
http://my.clients.com/GST/2015-4/LEBIN/Profiles/ClientProfile/Index/79
In my ajax method i have
$.ajax({
url: "ClientProfile/UpdateClient",
context: ..
data: ...
}).done(function() {
...;
});
therefore, using that format i get
http://my.clients.com/GST/2015-4/LEBIN/Profiles/ClientProfile/Index/79/ClientProfile/UpdateClient
But i need:
http://my.clients.com/GST/2015-4/LEBIN/Profiles/ClientProfile/UpdateClient
Also if i add a / before the url like this:
$.ajax({
url: "/ClientProfile/UpdateClient",
...
It formats the URL like this:
http://my.clients.com/UpdateClient
How can i obtain the correct path?, i just need to replace the /Index/83
with the correct action /UpdateClient
without funky replaces or logic..
ADDITIONAL INFO
The reson why i would not like to just add the whole url part.. is because /2015-4/ part might change several times
Based on my comment:
yourJsFile.js:
var AjaxCallToController = {
callAjax: function(url){
$.ajax({
url: url,
context: ..
data: ...
}).done(function() {
...;
});
}
}
And in yourView.cshtml
<script>
AjaxCallToController.callAjax('@Url.Action("UpdateClient", "ClientController"');
</script>
Something like that.
just use ../, that way you go back 1 folder, so it should look something like this
$.ajax({
url: "../../../ClientProfile/UpdateClient",
context: ..
data: ...
}).done(function() {
...;
});
You can achieve that by defining a default site URL
by adding it to the main template header then call it inside any .js
file as follow:
Define header URL:
<script> var site_url = '<?php echo site_url() ?>';</script>
// define the <?php echo site_url() ?> in your main PHP config file.
Then inside your javascript files, you can call that URL in different ways, for example:
window.location = site_url +'dashboard';
or
jQuery.post(site_url + 'account/forgot', jQuery(this).serialize(), function (res) {
// your code here
});
or
$.ajax({
type: "POST",
url: site_url + 'home/newsletters'
});
Thank you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.