代码之家  ›  专栏  ›  技术社区  ›  Muhammad Ashikuzzaman

查找Ajax的标题/任何标记错误.响应文本从它的Html字符串或转换的jQuery对象

  •  1
  • Muhammad Ashikuzzaman  · 技术社区  · 6 年前

    " “/”应用程序中存在服务器错误。
    找不到资源。 描述:HTTP 404。您正在查找的资源(或其 依赖项)可能已被删除,其名称已更改,或者 暂时不可用。请查看下面的网址并 /软件公司/HamdunSoft "

    因为上面的错误是通过ajax产生的 错误.响应文本

    error: function(error)
                    {
                    }
    

    完整的 错误.响应文本

    "
    <!DOCTYPE html>
    <html>
    
    <head>
      <title>The resource cannot be found.</title>
      <meta name="viewport" content="width=device-width" />
      <style>
        body {
          font-family: "Verdana";
          font-weight: normal;
          font-size: .7em;
          color: black;
        }
        
        p {
          font-family: "Verdana";
          font-weight: normal;
          color: black;
          margin-top: -5px
        }
        
        b {
          font-family: "Verdana";
          font-weight: bold;
          color: black;
          margin-top: -5px
        }
        
        H1 {
          font-family: "Verdana";
          font-weight: normal;
          font-size: 18pt;
          color: red
        }
        
        H2 {
          font-family: "Verdana";
          font-weight: normal;
          font-size: 14pt;
          color: maroon
        }
        
        pre {
          font-family: "Consolas", "Lucida Console", Monospace;
          font-size: 11pt;
          margin: 0;
          padding: 0.5em;
          line-height: 14pt
        }
        
        .marker {
          font-weight: bold;
          color: black;
          text-decoration: none;
        }
        
        .version {
          color: gray;
        }
        
        .error {
          margin-bottom: 10px;
        }
        
        .expandable {
          text-decoration: underline;
          font-weight: bold;
          color: navy;
          cursor: hand;
        }
        
        @media screen and (max-width: 639px) {
          pre {
            width: 440px;
            overflow: auto;
            white-space: pre-wrap;
            word-wrap: break-word;
          }
        }
        
        @media screen and (max-width: 479px) {
          pre {
            width: 280px;
          }
        }
      </style>
    </head>
    
    <body bgcolor="white">
    
      <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
    
                <h2> <i>The resource cannot be found.</i> </h2></span>
    
      <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
    
        <b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.
        <br><br>
    
        <b> Requested URL: </b>/Chemical/DyeingPartList<br><br>
    
        <hr width=100% size=1 color=silver>
    
        <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1038.0
    
      </font>
    
    </body>
    
    </html>
    <!-- 
    [HttpException]: A public action method &#39;DyeingPartList&#39; was not found on controller &#39;Menu.Controllers.ChemicalStore.ChemicalController&#39;.
       at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
       at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
       at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
       at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
       at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
       at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
       at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
       at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    -->"

    我把绳子扭了错误.响应文本从这个 answer .

    var element = $(error.responseText);
    

    元素是这样的

    enter image description here

    标题 在位置2的红色框中,索引为1。我能找到它(

     elem.get(1).innerHTML
     elem.get(1).text
     element[1].innerHTML
     element[1].text
    

    但是我想通过Jquery以下面的方式找到这个值。因为在将来的某些情况下,我可能需要按属性名而不是索引值来搜索对象中的元素。如果可能的话,请帮助我。

    $("title", element).html()
    $(element).find("title").html()
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Milind Anantwar    6 年前

    你可以用 $.parseHTML 将html字符串转换为DOM节点数组。它可以与jquery选择器和函数一起使用:

     var dom_nodes = $($.parseHTML(e.responseText));
     alert( dom_nodes.filter('title').text());
    
        2
  •  1
  •   jeetaz    6 年前

    How to create Document objects with JavaScript

    var doc = (new DOMParser).parseFromString(error.responseText, "text/html");
    // first argument: html to be converted to doc
    // second argument: mime_type (text/html or text/xml, it depends)
    

    第二,你知道规矩。

    doc.querySelector("title").textContent;
    // $(doc).find("title").html();
    // The resource cannot be found.
    

    或者,使用$.parseHTML和filter节点。

    var doc = $.parseHTML(error.responseText);
    var titleNode = doc.filter(function (node) {
        return node.localName === "title";
    });
    console.log(titleNode[0].textContent);