ajax + aspx(入门)

后端回传字串给前端
1.建立后端aspx
2.建立前端html (ajax呼叫aspx)

#ASPX
新增专案
新增aspx
把html都删掉,只留

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication2.aspx.WebForm1" %>

.cs的部分

        protected void Page_Load(object sender, EventArgs e)        {            Response.Write("hihi");            Response.End();        }

#ajax
新增html

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <script src="../js/jquery-3.2.1.js" type="text/javascript"></script>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title></head><body>    <div id="result"></div>    <script>        $(document).ready(function () {            $.ajax({                url: "WebForm1.aspx",                type: 'POST',                 success: function (data) {                    $('#result').text(data);                },                error: function (err) {                    console.log("err:");                    console.log(err);                    alert(err);                }            });        });    </script></body></html>

#测试
html 右键 在浏览器中检视 http://img2.58codes.com/2024/201067641BcDfibYVh.png
进阶
将js由html抽离开来
#js

$(document).ready(function () {    $.ajax({        url: "WebForm1.aspx",        type: 'POST',        success: function (data) {            $('#result').text(data);        },        error: function (err) {            console.log("err:");            console.log(err);            alert(err);        }    });});

#html
汇入js

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <script src="../js/jquery-3.2.1.js" type="text/javascript"></script>     <script src="js1.js" type="text/javascript"></script>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title></head><body>    <div id="result"></div> </body></html>

#测试
html 右键 在浏览器中检视 http://img2.58codes.com/2024/201067641BcDfibYVh.png


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章