gpt4 book ai didi

javascript - 从它的 Html 字符串或转换的 jQuery 对象中查找 Ajax error.ResponseText 的标题/任何标签

转载 作者:行者123 更新时间:2023-11-30 20:14:24 25 4
gpt4 key购买 nike

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

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

元素是这样的

enter image description here

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

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

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

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

最佳答案

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

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

关于javascript - 从它的 Html 字符串或转换的 jQuery 对象中查找 Ajax error.ResponseText 的标题/任何标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52071060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com