[英]Set Ajax Relative URL Correctly
I've got a MVC Application divided into areas and when I try to make a AJAX call from the Engineer area, the relative URL get transformed into a URL that does not match my intent;我有一个 MVC 应用程序划分为多个区域,当我尝试从工程师区域进行 AJAX 调用时,相对 URL 被转换为与我的意图不匹配的 URL;
Here is the example:这是示例:
This is URL on the Ajax call:这是 Ajax 调用上的 URL:
$.ajax({
cache: false,
type: 'GET',
url: 'api/JobData/GetSitesByClientId',
data: {
'clientId': selectedClient
}
})
This is the URL generated:这是生成的网址:
http://localhost:53433/Engineer/Jobs/api/JobData/GetSitesByClientId?clientId=09659dc4-faa7-4edb-af27-ecb7416a82fb
And this is the URL I wanted to generate:这是我想要生成的 URL:
http://localhost:53433/api/JobData/GetSitesByClientId?clientId=09659dc4-faa7-4edb-af27-ecb7416a82fb
How can I achieve this ?我怎样才能做到这一点?
Thanks in advance for your help在此先感谢您的帮助
Well you can put a leading slash /
at start of the URL.好吧,您可以在 URL 的开头放置一个前导斜杠/
。 However, I do not recommend this method because if your application gets deployed on a subdirectory (eg: www.host.com/myapp/
) , then the path would be wrong但是,我不推荐这种方法,因为如果您的应用程序部署在子目录中(例如: www.host.com/myapp/
),那么路径将是错误的
What I do in such case is to define a global rootPath variable in the javascript context of my view在这种情况下,我所做的是在我的视图的 javascript 上下文中定义一个全局 rootPath 变量
Layout布局
<script>
var rootPath = '@Url.Content("~")';
</script>
Then in Javascript files I append the rootPath to any path i want to use然后在 Javascript 文件中,我将 rootPath 附加到我想使用的任何路径
$.ajax({
url: rootPath + 'mycontroller/mymethod',
method: 'GET',
success:function(){
alert("success");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.