c# - 服务器响应 404 : Not Found for localhost/undefined

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

这几天我一直在摸不着头脑。我用谷歌搜索无济于事。我正在尝试使用 jquery.ajax 从数据库中获取内容,但基于我每次发送的信息(类似于发布和获取),服务器响应 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 的文件夹中。该脚本是从 Root/Views/SlideShow 文件夹中的 _SlideShowPartial.cshtml 文件调用的。该文件使用 @html.Partial()

加载到 Root/Views/Home 文件夹中的 Index.cshtml 文件中

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

最佳答案

您肯定向服务器发送了错误的 url。尝试完整的 url,或从 $.ajax 查询中删除 url。

关于c# - 服务器响应 404 : Not Found for localhost/undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30450008/

相关文章:

jquery - 如何将变量添加到对象键的末尾?

c# - IoC - 单一接口(interface)的多种实现支持

c# - 打印总和为特定值的所有子集

jquery - 事件冒泡和捕获的常见实际用途示例?

jquery - 悬停时更改滚动条宽度/颜色?

php - 使用 jquery post 请求到同一页面

javascript - 获取ajax请求响应node.js

java - 等效于 Java 中的 C# 匿名方法?

c# - 快速连续的随机数

php - 用于传递 PHP 变量的 Javascript 语法