Home  >  Article  >  Web Front-end  >  How to get javascript data from aspx response

How to get javascript data from aspx response

WBOY
WBOYOriginal
2023-05-17 14:06:37687browse

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.

  1. First add a HiddenField control in the ASPX file:
<asp:HiddenField ID="hdnData" runat="server" />
  1. Generate the JavaScript data that needs to be passed in the ASPX.cs file and store the data in In HiddenField:
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:

  1. Use the write method of the Document object to write JavaScript code in the page, and access the data returned by the ASPX.cs file in the code. For example:
<script type="text/javascript">
    document.write('<script type="text/javascript" src="js/myjs.js"><'+'/script>');
    var data = '<%= hdnData.Value %>';
    // 在这里对数据进行处理
</script>
  1. Use the XMLHttpRequest object in JavaScript to send a request to the ASPX page, and then get the returned data in the callback function. For example:
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

  1. The data returned in the ASPX page needs to meet JavaScript syntax rules, otherwise it will cause JavaScript code execution to fail, ultimately affecting the normal operation of the entire application.
  2. When obtaining the Response data returned by ASPX, it should be noted that the Response data is not necessarily pure JavaScript code. It may contain other types of data such as HTML, CSS, XML, etc., and needs to be parsed in JavaScript. and processing.
  3. In order to avoid cross-domain problems, usually when requesting data from an ASPX page in JavaScript, the request needs to be sent to the ASPX page under the same domain name. Otherwise, the data may not be obtained due to browser restrictions.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn