Home > Article > Web Front-end > How to get javascript data from aspx response
In ASP.NET development, you usually encounter situations where you need to obtain the data returned by the server in JavaScript, and these data are often generated and returned in the ASPX Response. This article will introduce how to obtain JavaScript data from ASPX Response.
1. Generating JavaScript data in ASPX pages
There are many ways to generate JavaScript data in ASPX pages. This article briefly introduces a common way, which is to store data through the HiddenField control. in the page and then fetched and processed in JavaScript.
<asp:HiddenField ID="hdnData" runat="server" />
protected void Page_Load(object sender, EventArgs e) { // 生成需要传递的数据 string data = "hello world"; // 将数据存储在HiddenField中 hdnData.Value = data; }
2. Obtain ASPX Response data in JavaScript
The Response returned by ASPX contains a lot of information. If we need to obtain the JavaScript data, we can In the following ways:
<script type="text/javascript"> document.write('<script type="text/javascript" src="js/myjs.js"><'+'/script>'); var data = '<%= hdnData.Value %>'; // 在这里对数据进行处理 </script>
function loadData() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var data = xmlhttp.responseText; // 在这里对数据进行处理 } }; xmlhttp.open("GET", "myPage.aspx", true); xmlhttp.send(); }
Both of the above two methods can obtain the JavaScript data returned by the ASPX page. The difference is that the first method is to obtain the data directly when the page is loaded, and the second method is to obtain the data in the JavaScript Obtain data through asynchronous requests.
3. Notes
In short, getting JavaScript data from ASPX Response requires generating and returning the original data in the ASPX page, and then parsing and processing it in JavaScript. Through the above form, we can easily realize the front-end and back-end data transfer in ASP.NET applications.
The above is the detailed content of How to get javascript data from aspx response. For more information, please follow other related articles on the PHP Chinese website!