Pra*_*bhu 1 .net javascript c# asp.net-mvc jquery
目前我在我的js文件中做的是这个(它的工作原理):
var root = "http://mydomain.com";
$.ajax({
type: "POST",
url: root + "/MyController/MyAction",
data: { 'id': myId },
dataType: "html",
success: function (response) {
blah blah...
Run Code Online (Sandbox Code Playgroud)
但问题是如果有人在www.mydomain.com而不是mydomain.com中输入,则找不到路径.我尝试按照这篇文章中的建议:Javascript文件中的相对图像URL - ASP.net MVC和IIS 7,即将root设置为../或document.location.host,但两者都不起作用.
在js文件中指定路径(控制器,图像等中的操作)的正确方法是什么?
谢谢.
在你看来:
<script type="text/javascript">
var url = '<%= Url.Action("MyAction", "MyController") %>';
</script>
Run Code Online (Sandbox Code Playgroud)
并在您的外部JavaScript文件中:
$.ajax({
type: "POST",
url: url,
data: { 'id': postId },
dataType: "html",
success: function (response) {
blah blah...
Run Code Online (Sandbox Code Playgroud)
甚至更好:如果你是AJAX化现有的链接或形式:
<%= Html.ActionLink("foo", "MyAction", "MyController", null, new { id = "foo" })
Run Code Online (Sandbox Code Playgroud)
并在您的JavaScript中:
$('#foo').click(function() {
$.ajax({
type: "POST",
url: this.href,
data: { 'id': postId },
dataType: "html",
success: function (response) {
blah blah...
});
return false;
});
Run Code Online (Sandbox Code Playgroud)