Home > Web Front-end > JS Tutorial > body text

ajax demo source code

韦小宝
Release: 2018-03-12 10:54:50
Original
1864 people have browsed it

This article introduces the ajax operation in the basic native JavaScript. The content is relatively basic. Students who do not have a clear grasp of ajax in JavaScript can take a look at the ajax operation in JavaScript again. , not so much nonsense, let’s take a look!
The following html files must be opened in a server environment.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ceshi</title>
    <script type="text/javascript" src="ajax.js"></script>

</head>
<body>
    帐号:<input type="text" id="username">
    密码:<input type="password" id="password">
    <input type="button" value="提交" onclick="checkname()">
</body>

<script type="text/javascript">
    function checkname() {
        var username = document.getElementById(&#39;username&#39;).value;
        //1、创建对象
        var xhr = new XMLHttpRequest();
        //2、连接服务器
        xhr.open(&#39;GET&#39;,&#39;name.json&#39;,true);//json数据内容如下图3

        //3、向服务器发送请求
        xhr.send(null);
        //4、请求完成,响应就绪
        xhr.onreadystatechange=function(){
        if (xhr.readyState==4) {
            if (xhr.status==200) {//表示已经获取到文件。
                var str = JSON.parse(xhr.responseText);//将json数据转换成js数组对    
                   alert(str);//输出结果如图4 。               
            }else{
                alert(xhr.statusText)//如果没有获取name.json的数据,即服务器找不到这个文件,则执行该段代码。输出框会显示“Not Found”。如果是alert(xhr.status),则显示404;
                }

            }
        }
        };


</script>
</html>
Copy after login

ajax demo source code

ajax demo source code

<script type="text/javascript">
    function checkname() {
        var username = document.getElementById(&#39;username&#39;).value;
        var boo = false;
        //1、创建对象
        var xhr = new XMLHttpRequest();
        //2、连接服务器
        xhr.open(&#39;GET&#39;,&#39;name1.json&#39;,true);
        //3、向服务器发送请求
        xhr.send(null);
        //4、请求完成,响应就绪
        xhr.onreadystatechange=function(){
        if (xhr.readyState==4) {
            if (xhr.status==200) {
                var str = JSON.parse(xhr.responseText);//将json数据转换成js数组对象
                alert(str)                  
             for (var i = 0; i < str.length; i++) {
                if (username == str[i]) {
                    boo = true;
                }
             }//判断用户名是否已经存在,即输入的用户名是否存在与json文件中。
            if (boo) {
               alert("用户民已存在");
            } else {
                alert("用户名可以使用")
            }

            }
        }
        };
     };
</script>
Copy after login

ajax technology is a technology that can bring the experience of desktop applications to web applications. This experience effect is mainly the exchange of data without refreshing the page and changing the content without refreshing the page. There are really many functions of ajax, and there are so many that I can’t finish talking about.

Recommended related ajax articles:

Ajax and PHP instance analysis

Tips on using jq to send multiple ajax and then execute callbacks

The above is the detailed content of ajax demo source code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template