다음과 같은 코드가 있습니다 :전체 HTML 페이지가 포함 된 Ajax 응답의 본문 태그 내에있는 내용 만 빼내려면 어떻게해야합니까?
$.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를 사용하여 본문 태그의 내용을 어떻게 파싱 할 수 있습니까?
<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> 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.<>c__DisplayClassd.<InvokeActionMethodWithFilters>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.<>c__DisplayClass8.<BeginProcessRequest>b__4() +58
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371
</pre></code>
</td>
</tr>
</table>
<br>
<hr width=100% size=1 color=silver>
<b>Version Information:</b> 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을 문제를 해결하는 방법을 알고 있을까요?
+1 그게 효과가 있습니다. 참고로, 테스트가 끝나면 myFrame을 할당하는 코드를 줄일 수 있어야합니다. 할당 된 값은 동일합니다. 다음과 같이 논리 연산자 '||'연산자를 사용할 수 있습니다 :'myFrame = myFrame.contentWindow || myFrame.contentDocument.document || myFrame.contentDocument;'발견 한 첫 번째 것을 할당합니다. – user113716
+1 바로 뒤로. – Chev