gpt4 book ai didi

jquery - 如何仅提取包含整个 HTML 页面的 ajax 响应的 body 标记内的内容?

转载 作者:行者123 更新时间:2023-11-28 05:08:07 26 4
gpt4 key购买 nike

我有以下代码:

$.ajax(
{
url: rootPath + "Framework/GetPartial",
data: { partialName: partialName },
type: "POST",
success: function (response)
{
$('#loading').hide();
$('#partialContent').hide().html(response).fadeIn();
},
error: function (xhr, textStatus, errorThrown)
{
$('#loading').hide();
$('#partialContent').hide().html('An error occurred. Details below:<br /><br />' + xhr.responseText).fadeIn();
}
});

我关心的是错误处理函数。当服务器发生错误时,将发送一个包含详细调试信息的 html 页面作为响应。我想将此 html 响应加载到现有页面中,但包含 <HTML> , <HEAD> , 和 <BODY>响应中的标签会破坏整个页面的格式。我如何在以下 HTML 响应中使用 JQuery 仅解析 body 标记中的内容?

<html>

<head>

<title>The method or operation is not implemented.</title>

<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:"Lucida Console";font-size: .9em}

.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; }

</style>

</head>



<body bgcolor="white">



<span><H1>Server Error in '/AlexAndNikki' Application.<hr width=100% size=1 color=silver></H1>



<h2> <i>The method or operation is not implemented.</i> </h2></span>



<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">



<b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.



<br><br>



<b> Exception Details: </b>System.NotImplementedException: The method or operation is not implemented.<br><br>



<b>Source Error:</b> <br><br>



<table width=100% bgcolor="#ffffcc">

<tr>

<td>

<code><pre>



Line 19: public PartialViewResult GetPartial(string partialName)

Line 20: {

<font color=red>Line 21: throw new NotImplementedException();

</font>Line 22: //System.Threading.Thread.Sleep(3000);

Line 23: if (!ViewExists(partialName))</pre></code>



</td>

</tr>

</table>



<br>



<b> Source File: </b> C:\Users\alex.ford\Documents\Visual Studio 2010\Projects\AlexAndNikki\AlexAndNikki\Controllers\FrameworkController.cs<b> &nbsp;&nbsp; Line: </b> 21

<br><br>



<b>Stack Trace:</b> <br><br>



<table width=100% bgcolor="#ffffcc">

<tr>

<td>

<code><pre>



[NotImplementedException: The method or operation is not implemented.]

AlexAndNikki.Controllers.FrameworkController.GetPartial(String partialName) in C:\Users\alex.ford\Documents\Visual Studio 2010\Projects\AlexAndNikki\AlexAndNikki\Controllers\FrameworkController.cs:21

lambda_method(Closure , ControllerBase , Object[] ) +127

System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +258

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39

System.Web.Mvc.&lt;&gt;c__DisplayClassd.&lt;InvokeActionMethodWithFilters&gt;b__a() +125

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +640

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +312

System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709

System.Web.Mvc.Controller.ExecuteCore() +162

System.Web.Mvc.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__4() +58

System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +20

System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453

System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +371

</pre></code>



</td>

</tr>

</table>



<br>



<hr width=100% size=1 color=silver>



<b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1



</font>



</body>

</html>

<!--

[NotImplementedException]: The method or operation is not implemented.

at AlexAndNikki.Controllers.FrameworkController.GetPartial(String partialName) in C:\Users\alex.ford\Documents\Visual Studio 2010\Projects\AlexAndNikki\AlexAndNikki\Controllers\FrameworkController.cs:line 21

at lambda_method(Closure , ControllerBase , Object[] )

at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)

at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)

at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()

at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)

at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

at System.Web.Mvc.Controller.ExecuteCore()

at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4()

at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()

at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

-->

编辑:

有没有办法将响应字符串加载到 iframe 中?如果可能的话,那就太好了。

类似于:

$('#partialContent').html('<iframe>' + xhr.responseText + '</iframe>');

显然我试过这段代码但没有用,但也许有人知道如何制作 iframe 来解决问题?

最佳答案

你可以这样做:

$('#partialContent').hide().html($('body',response).contents()).fadeIn();

这是 .empty().append() 的快捷方式在下面,但主要部分是 $('body',response) ,正在寻找 <body>响应中的元素,然后将其内容作为要追加的元素。

关于jquery - 如何仅提取包含整个 HTML 页面的 ajax 响应的 body 标记内的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4261454/

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