zoukankan      html  css  js  c++  java
  • ajax 请求常用写法

    一、jquery.ajax()

    function checkSubmit()
        {
            var formData = new FormData($('#ImportForm')[0]);//Form表单数据
            url = '@Url.Action("ImportQuestionExcel","Common")' + "?time=" + new Date();
            console.log(url);
            $.ajax({
                type:"POST",
                url:url,
                data: formData,
                processData: false,//不处理数据
                contentType: false,//不定义类型 因为上传了文件
                success:function(data){
                    Import_OnSuccess(data);
                },
                error:function(){
                    $(document).alertmsg('confirm',"处理异常");
                }
            });
        }
    View Code

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            var xmlhttp = createXHR();
            function createXHR() {
                var xhr;
                if (window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                } else {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                return xhr;
            }

            function login() {
                var span = document.getElementById("msg");
                span.innerHTML = "正在登录...";

                var name = document.getElementById("txtName").value;
                var pwd = document.getElementById("txtPwd").value;
                var postData = "n=" + name + "&p=" + pwd;

                xmlhttp.open("post", "02-post.ashx", true);
                //!!!!!!!!!!!!!!!!!!!post请求,必须设置Content-Type
                xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4) {
                        if (xmlhttp.status == 200) {
                            var r = xmlhttp.responseText;
                            
                            if (r == 1) {
                                span.innerHTML = "登录成功";
                            } else if (r == 0) {
                                span.innerHTML = "登录失败";
                            } else {
                                span.innerHTML = "未知错误";
                            }
                        }
                        else {
                            span.innerHTML = "服务器内部错误";
                        }
                    }
                }
                xmlhttp.send(postData);
            }
        </script>
    </head>
    <body>
        用户名:<input type="text" id="txtName" /><br />
        密码:<input type="text" id="txtPwd" /><br />
        <input type="button" id="btnLogin" value="Login" onclick="login()" /><span style="color:Red" id="msg"></span>
    </body>
    </html>

  • 相关阅读:
    IIS网站应用偶尔出现"服务不可用"或者显示乱码字体
    mac os下切换pip3国内源并安装requests库
    mysql5.6运行一段时间之后网站页面出现乱码解决办法
    mac pro下安装安装 SymPy 和 matplotlib报错解决方案
    python3汉诺塔简单实现代码
    用python提取xml里面的链接源码
    Mac环境下 jieba 配置记录
    AngularJS 整理学习
    Java有关List的stream基本操作
    Callable的简单使用
  • 原文地址:https://www.cnblogs.com/eric-gms/p/3470925.html
Copyright © 2011-2022 走看看