从 Html 字符串或已转换的 jQuery 对象中找到 Ajax error.ResponseText 的 title/any 标签

Find title/any tag of Ajax error.ResponseText from it's Html string or Converted jQuery Object

我想通过 jQuery ajax 执行控制器操作,但我并没有故意执行该操作来显示错误。

" Server Error in '/' Application.
The resource cannot be found. Description: 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. Please review the following URL and make sure that it is spelled correctly. Requested URL: /SoftwareCompany/HamdunSoft "

由于上述错误来自 ajax error.responseText

error: function(error)
                {
                }

完整的error.responseText在下面的代码片段中。

"
<!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)
-->"

我已将 error.responseText 中的字符串 answer 转换为对象元素。

var element = $(error.responseText);

元素是这样的

正如我们所见,红色框中的对象中有一个名为 title 的元素,位置 2 索引为 1。我可以找到它的(title 标签) innerHtml 通过以下任何一项

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

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

$("title", element).html()
$(element).find("title").html()

首先,从字符串创建一个文档。

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 和过滤器节点。

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

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

 var dom_nodes = $($.parseHTML(e.responseText));
 alert( dom_nodes.filter('title').text());