如何在JavaScript中处理一个ModelAndView对象

huangapple go评论57阅读模式
英文:

how to handle a ModelAndView object with java script

问题

页面正常渲染,但与按钮绑定的功能停止工作,单击无响应。尝试将JavaScript从外部文件添加到HTML页面中也没有解决问题。这可能与事件监听器中断有关。您可以尝试重新加载JavaScript或刷新事件监听器来解决这个问题。

重新加载JavaScript或刷新事件监听器的方法取决于您的具体情况。您可以尝试以下方法:

  1. 在页面加载后重新执行JavaScript代码。您可以在HTML文档底部添加以下代码:
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 在这里重新执行您的JavaScript代码,包括事件监听器设置
        // 例如,您可以再次添加事件监听器
    });
</script>
  1. 如果您使用的JavaScript依赖于文档的特定元素,请确保在重新加载页面内容后重新设置事件监听器。这通常需要在渲染新内容后调用JavaScript函数来重新设置事件监听器。

请注意,具体的解决方法取决于您的JavaScript代码和HTML结构,您可能需要调试以找到问题的准确原因。

英文:

i have a controller that returns a ModelAndView, this controller is called with this script:

	document.getElementById(&#39;history-equipment&#39;).addEventListener(&#39;click&#39;, function(event) {
			event.preventDefault(); // Prevent the default anchor tag behavior (e.g., navigating to a new page)

			const url = &#39;/View/Student/History&#39;; // Replace with your API endpoint URL
			var token = localStorage.getItem(&quot;token&quot;);
			// Request headers
			const headers = {
				&#39;Content-Type&#39;: &#39;application/json&#39;,
				&#39;Authorization&#39;: token.valueOf() // Replace with your actual access token
			};

			// Request options
			const requestOptions = {
				method: &#39;GET&#39;, // Replace with the HTTP method you want to use (e.g., GET, POST, PUT, DELETE)
				headers: headers
			};

			// Send the request
			fetch(url, requestOptions)
					.then(response =&gt; response.text()) // Use response.text() instead of response.json() to get the HTML content
					.then(data =&gt; {
						// Handle the response data
						console.log(data);

						// Render the returned model and view
						renderModelAndView(data);
					})
					.catch(error =&gt; {
						// Handle any errors
						console.error(&#39;Error:&#39;, error);
					});
		});

		function renderModelAndView(htmlContent) {
			// Replace the entire HTML page with the received content
			document.open();
			document.write(htmlContent);
			document.close();
		}

and this the html template i want to render:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;!-- icons link--&gt;
&lt;link href=&#39;https://unpkg.com/boxicons@2.0.9/css/boxicons.min.css&#39; rel=&#39;stylesheet&#39;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.4/font/bootstrap-icons.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot;&gt;
&lt;!-- custom css link--&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;../../static/css/style.css&quot;&gt;
&lt;title&gt;Request Equipment&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!-- SIDEBAR --&gt;
&lt;section id=&quot;sidebar&quot;&gt;
&lt;a href=&quot;#&quot; class=&quot;logo&quot;&gt;&lt;img src=&quot;../../static/css/images/logo.png&quot; class=&quot;logo-pic&quot;&gt; Smart EMS&lt;/a&gt;
&lt;ul class=&quot;side-menu&quot;&gt;
&lt;li&gt;&lt;a  href=&quot;&quot; &gt;&lt;i class=&#39;bx bx-devices icon&#39;&gt;&lt;/i&gt; Allocation History&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a  href=&quot;/View/Student/Request&quot; &gt;&lt;i class=&#39;bx bx-devices icon&#39;&gt;&lt;/i&gt; Request Equipment &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/section&gt;
&lt;!-- NAVBAR --&gt;
&lt;section id=&quot;content&quot;&gt;
&lt;nav&gt;
&lt;i class=&#39;bx bx-menu toggle-sidebar&#39; &gt;&lt;/i&gt;
&lt;form action=&quot;#&quot;&gt;&lt;/form&gt;
&lt;div id=&quot;toggle-btn&quot; class=&quot;fas fa-sun&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;notif&quot;&gt;
&lt;i class=&quot;bi bi-bell-fill nav icon&quot; id=&quot;bell&quot;&gt;&lt;/i&gt;
&lt;ul class=&quot;notif-link&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; &gt;&lt;i class=&quot;bi bi-bell-fill icon &quot;style=&quot;color: green;&quot;&gt;&lt;/i&gt;check&lt;/a&gt;&lt;/li&gt;
&lt;hr&gt;
&lt;li&gt;&lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;bi bi-bell-fill icon&quot; style=&quot;color: green;&quot;&gt;&lt;/i&gt;check&lt;/a&gt;&lt;/li&gt; 
&lt;hr&gt;
&lt;li&gt;&lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;bi bi-bell-fill icon&quot; style=&quot;color: green;&quot;&gt;&lt;/i&gt;check&lt;/a&gt;&lt;/li&gt;       
&lt;/ul&gt;
&lt;/div&gt;
&lt;span class=&quot;divider&quot;&gt;&lt;/span&gt;
&lt;div class=&quot;profile&quot;&gt;
&lt;img src=&quot;../../static/css/images/student.png&quot; alt=&quot;&quot;&gt;
&lt;ul class=&quot;profile-link&quot;&gt;
&lt;li&gt;&lt;a href=&quot;Student-ManageMyAccount.html&quot; &gt;&lt;i class=&quot;bi bi-person-circle icon&quot;&gt;&lt;/i&gt;Update info&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../Login%20&amp;%20Sign-up.html&quot;&gt;&lt;i class=&quot;bi bi-box-arrow-in-left&quot;&gt;&lt;/i&gt; Logout&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;!-- MAIN --&gt;
&lt;main&gt;
&lt;h1 class=&quot;title&quot;&gt;Request Equipment&lt;/h1&gt;
&lt;ul class=&quot;snippet&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;divider&quot;&gt;/&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; class=&quot;active&quot;&gt; Request Equipment &lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;div class=&quot;data&quot;&gt;
&lt;div class=&quot;content-data&quot;&gt;
&lt;!-- data table --&gt;
&lt;div class=&quot;head&quot;&gt;
&lt;h3&gt;Equipment Details&lt;/h3&gt;
&lt;/div&gt; 
&lt;div class=&quot;table-wrapper&quot;&gt;
&lt;table class=&quot;fl-table&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Item ID&lt;/th&gt;
&lt;th&gt;Serial number&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;Content 1&lt;/td&gt;
&lt;td&gt;Content 1&lt;/td&gt;
&lt;td&gt;Content 1&lt;/td&gt;
&lt;td&gt;
&lt;a href=&quot;Student-SendRequestEquipment.html&quot; class=&quot;table-button1&quot;&gt;Request&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;Content 2&lt;/td&gt;
&lt;td&gt;Content 2&lt;/td&gt;
&lt;td&gt;Content 2&lt;/td&gt;
&lt;td&gt;
&lt;a href=&quot;Student-SendRequestEquipment.html&quot; class=&quot;table-button1&quot;&gt;Request&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;Content 3&lt;/td&gt;
&lt;td&gt;Content 3&lt;/td&gt;
&lt;td&gt;Content 3&lt;/td&gt;
&lt;td&gt;
&lt;a href=&quot;Student-SendRequestEquipment.html&quot; class=&quot;table-button1&quot;&gt;Request&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;Content 4&lt;/td&gt;
&lt;td&gt;Content 4&lt;/td&gt;
&lt;td&gt;Content 4&lt;/td&gt;
&lt;td&gt;
&lt;a href=&quot;Student-SendRequestEquipment.html&quot; class=&quot;table-button1&quot;&gt;Request&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;#&lt;/td&gt;
&lt;td&gt;Content 5&lt;/td&gt;
&lt;td&gt;Content 5&lt;/td&gt;
&lt;td&gt;Content 5&lt;/td&gt;
&lt;td&gt;
&lt;a href=&quot;Student-SendRequestEquipment.html&quot; class=&quot;table-button1&quot;&gt;Request&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/main&gt;
&lt;/section&gt;
&lt;!--custom js link--&gt;
&lt;script src=&quot;../../static/js/script.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;../../static/js/script2.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

the page is rendered normally, but the functions tied to the buttons stop working, i click and nothing happens. i tried taking the Js from an external file and adding it to the html page itself and that didn't work. i assume it has something to do with eventListeners breaking. So is there anyway way to load the javaScript or refresh the eventListeners?

答案1

得分: 0

我假设您从服务器获取了一些HTML代码作为字符串。您可以尝试使用iframe来嵌入这段代码。但首先,您需要在调用此URL的文档中创建一个带有iframe的HTML主体。

var doc = document.getElementById('iframe').contentWindow.document;
doc.open();
doc.write(htmlContent);
doc.close();
英文:

I assume you are getting some html code as a string from server. you can try using iframe to embed this code. but first you need to create an html body with iframe in in the document from which you are calling this url.

var doc = document.getElementById(&#39;iframe&#39;).contentWindow.document;
doc.open();
doc.write(htmlContent);
doc.close();

答案2

得分: 0

我解决了这个问题。问题出在事件监听器在文档被重写时中断。我用从HTML文件调用的函数替换了事件监听器。

英文:

i fixed the problem. it was an issue of eventListeners breaking when the document is rewritten. i replaced the eventListeners with functions that i call from the html file

huangapple
  • 本文由 发表于 2023年5月14日 12:45:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定