weixin_33724046 2015-06-05 08:55 采纳率: 0%
浏览 44

404找不到Ajax jQuery MVC

I created an MVC3 project in Visual Studio 2013. The view engine is razor.First I write jquery.ajax in @section Scripts {} in view(cshtml) it works fine. But I seperate script in .js file and debugging I get error : (debugging IIS Express)

IIS 8.0 Detailed Error - 404.0 - Not Found ... More Information: This error means that the file or directory does not exist on the server. Create the file or directory and try the request again.
..

My jquery function(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);
        }
    });
}

}

Controller :

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);
    }

I tried this but not work (I got a new error: resource cannot be found ):

  <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>

please help..

  • 写回答

2条回答 默认 最新

  • python小菜 2015-06-05 08:59
    关注

    You can't use Razor inside a JavaScript file (unless you use something like the RazorJS extension). If you look at your JS in the browser dev tools you'll see that the url outputted is:

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

    When it should be:

    url: www.yoursite.com/Archive/GetProjects
    

    To fix it, you could make a method call from your view to that JS and pass in the URL, something like:

    View:

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

    Then your JS would be:

    function callMethod(url) {
       ....
    

    Or failing that, you code hard code it to be:

    url: '/Archive/GetProjects'
    

    But be careful if you're deploying to a site within a sub-directory, that link won't be valid.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比