2015-06-05 161 views
2

我在Visual Studio 2013中創建了一個MVC3項目。視圖引擎是razor.First我在@section Scripts {}視圖(cshtml)中編寫了jquery.ajax,它工作正常。但我單獨腳本.js文件和調試,我得到錯誤: (IIS調試快遞)404 Not Found Ajax jquery MVC

IIS 8.0詳細錯誤 - 404.0 - 未找到 ... 更多信息:此錯誤意味着該文件或目錄不在服務器上不存在。創建文件或目錄並再次嘗試請求。
..

我的jQuery函數(archive.js):

jQuery.AjaxGetProjects = function (dropdownId, detailDropDownId, authorized) { 

var projectId = $(dropdownId).val(); 
if (projectId != null && projectId != '') { 
    var url = 
    $.ajax({ 
     type: "POST", 
     url: '@Url.Action("GetProjects", "Archive")', 
     data: { 
      'projectId': projectId, 
      'authorized': authorized 
     }, 
     success: function (departman) { 
      var length = 0; 
      $(detailDropDownId).empty(); 
      $.each(departman, function (index, proje) { 
       length = length + 1; 
       $(detailDropDownId).append($('<option/>', { 
        value: proje.Value, 
        text: proje.Text, 
        selected: proje.Selected 
       })); 
      }); 
      if (length == 2) { 
       $(detailDropDownId).trigger('change'); 
      } 

     }, 
     error: function (xhr, ajaxOptions, thrownError) { 
      // bu kısımda eğer ajax işlemi başarısız ise 
      // hata mesajı verebiliriz. 
      alert(xhr.responseText); 
     } 
    }); 
} 

}

控制器:

public class ArchiveController : BaseController 
{ 
    ... 
    public ActionResult GetProjects(int projectId, bool authorized) 
    { 

     IArchive arch = WcfServiceHandler.GetDmsService<IArchive>(); 

     List<Poco> list = arch.GetProjects(UserManager.GetUserInfo(), projectId); 
     var t = MvcHelper.GetDropDownList<Poco>(list, "NAME", "ID", ""); 
     return Json(t, JsonRequestBehavior.AllowGet); 
    } 

我試過,但無法正常工作(我有一個新的錯誤:資源無法找到):

<system.webServer> 
<validation validateIntegratedModeConfiguration="false" /> 
<modules runAllManagedModulesForAllRequests="true" /> 
<handlers> 
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" /> 
</handlers> 

請幫助..

+1

' '@ Url.Action( 「GetProjects」, 「歸檔」)','是剃刀代碼。 Razor代碼不在外部腳本文件中分析。您需要在主視圖中定義一個變量,然後從腳本文件中訪問該變量 –

+0

@StephenMuecke您可以舉例。我在MVC中是全新的。 –

+0

在主視圖'var myUrl ='@ Url.Action(「GetProjects」,「Archive」)';'和腳本文件'$ .ajax({url:myUrl,....' –

回答

4

不能使用剃刀一個JavaScript文件中(除非你使用類似RazorJS擴展名)。如果你看一下在瀏覽器開發者工具的JS,你會看到輸出的網址是:

url: '@Url.Action("GetProjects", "Archive")', 

當它應該是:

url: www.yoursite.com/Archive/GetProjects 

爲了解決這個問題,你可以做一個方法調用從您的視圖到JS和通過在URL中,是這樣的:

查看:

<script> 
    callMethod('@Url.Action("GetProjects", "Archive")'); 
</script> 

那麼你的JS會是:

function callMethod(url) { 
    .... 

如果不行的話,你的代碼硬編碼爲:

url: '/Archive/GetProjects' 

但如果你是一個子目錄內部署到網站要小心,該鏈接不會有效。

+0

我們也可以**添加參數** check [this](http://stackoverflow.com/a/25050306/2218697) – stom

1

javascript無法使用razor語法,因此@ Url.Action(「GetProjects」,「Archive」)將不起作用,請嘗試將post URL作爲參數傳遞或硬編碼。

jQuery.AjaxGetProjects = function (dropdownId, detailDropDownId, authorized) { 

var projectId = $(dropdownId).val(); 
if (projectId != null && projectId != '') { 
var url = 
$.ajax({ 
    type: "POST", 
    url: '/Archive/GetProjects', 
    data: { 
     'projectId': projectId, 
     'authorized': authorized 
    }, 
    success: function (departman) { 
     var length = 0; 
     $(detailDropDownId).empty(); 
     $.each(departman, function (index, proje) { 
      length = length + 1; 
      $(detailDropDownId).append($('<option/>', { 
       value: proje.Value, 
       text: proje.Text, 
       selected: proje.Selected 
      })); 
     }); 
     if (length == 2) { 
      $(detailDropDownId).trigger('change'); 
     } 

    }, 
    error: function (xhr, ajaxOptions, thrownError) { 
     // bu kısımda eğer ajax işlemi başarısız ise 
     // hata mesajı verebiliriz. 
     alert(xhr.responseText); 
    } 
}); 

}

OR

jQuery.AjaxGetProjects = function (dropdownId, detailDropDownId, authorized, postUrl) { 

var projectId = $(dropdownId).val(); 
if (projectId != null && projectId != '') { 
var url = 
$.ajax({ 
    type: "POST", 
    url: postUrl, 
    data: { 
     'projectId': projectId, 
     'authorized': authorized 
    }, 
    success: function (departman) { 
     var length = 0; 
     $(detailDropDownId).empty(); 
     $.each(departman, function (index, proje) { 
      length = length + 1; 
      $(detailDropDownId).append($('<option/>', { 
       value: proje.Value, 
       text: proje.Text, 
       selected: proje.Selected 
      })); 
     }); 
     if (length == 2) { 
      $(detailDropDownId).trigger('change'); 
     } 

    }, 
    error: function (xhr, ajaxOptions, thrownError) { 
     // bu kısımda eğer ajax işlemi başarısız ise 
     // hata mesajı verebiliriz. 
     alert(xhr.responseText); 
    } 
}); 
} 
+0

謝謝@ SE23。 –