服务器响应404:找不到localhost / undefined

时间:2015-05-26 05:17:33

标签: c# jquery ajax asp.net-mvc-4

我好几天都在摸不着头脑。我用google搜索无济于事。我试图从jquery.ajax的数据库中获取东西,基于我每次发送的信息(sorta a post和a get),服务器以Error 404: Not Found响应,并告诉我我要求Localhost:1986/undefined,情况并非如此。

我确定我的所有文件夹和文件的确切位置非常重要,因此我会尽可能多地提供,如果有必要,请随时提出更多要求。

我的脚本是从我必须发布到数据库的另一个脚本中复制粘贴和修改的,并且工作正常,所以为什么这个不会让我感到困惑:

function nextSlide() {
    var articleLink = document.getElementsByTagName("a")[0];
    $.ajax({
        type: 'POST',
        url: "../SlideShow/NextArticle",
        data: {
            slideID: articleLink.getAttribute("id"),
        },
        success: function (result) {
            $(articleLink).id = result[0];
            document.getElementById("articleImage").setAttribute("src", result[1]);
            document.getElementById("articleTitle").innerHTML = result[2];
            document.getElementById("articleText").innerHTML = result[3];
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus); alert("Error: " + errorThrown);
        }
    });
}

这是我尝试使用的SlideShowController.cs文件中的方法:

[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public JsonResult NextArticle(string slideID)
{
    int ID = int.Parse(slideID);
    var articles = db.Slides.ToList();
    var currentArticle = db.Slides.First(s => s.SlideId == ID);
    int articlePosition = articles.IndexOf(currentArticle);
    articlePosition = (articlePosition + 1) >= articles.Count() ? 0 : articlePosition + 1;

    var nextArticle = articles.ElementAt(articlePosition);
    //nextArticle.Article.ArticleText.Substring(0, 50)+"..."
    string[] returnParameters = { 
                                    nextArticle.SlideId.ToString(),
                                    nextArticle.Image.ImageURL,
                                    nextArticle.Article.ArticleTitle,
                                    nextArticle.Article.ArticleText
                                };
    return Json(returnParameters);
}

SlideShowController.cs文件位于根目录中名为Controllers的文件夹中。该脚本是从_SlideShowPartial.cshtml文件夹中的Root/Views/SlideShow文件调用的。该文件使用Index.cshtml

加载到Root/Views/Home文件夹中的@html.Partial()文件中

以下是Chrome控制台中显示的错误的屏幕截图: enter image description here

以下是Views文件夹中的Web.Config文件的内容:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor"     type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup,     System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral,     PublicKeyToken=31BF3856AD364E35">
      <section name="host"     type="System.Web.WebPages.Razor.Configuration.HostSection,     System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral,     PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages"     type="System.Web.WebPages.Razor.Configuration.RazorPagesSection,     System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral,     PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,     Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="TIAWebWorkConnect.Models.AppViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

    <!--
        Enabling request validation in view pages would cause validation to     occur
        after the input has already been processed by the controller. By default
        MVC performs request validation before a controller processes the input.
        To change this behavior apply the ValidateInputAttribute to a
        controller or action.
    -->
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter,     System.Web.Mvc, Version=4.0.0.0, Culture=neutral,     PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0,     Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc,     Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral,     PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

2 个答案:

答案 0 :(得分:0)

你肯定错误地将url发送到服务器。尝试完整的网址,或从$ .ajax查询中删除网址。

答案 1 :(得分:0)

考虑将T4 MVC引入您的项目。这将为您提供对MVC操作的强类型访问,并允许您避免看起来像您的问题的URL字符串。

https://github.com/T4MVC/T4MVC/blob/master/README.md