zoukankan      html  css  js  c++  java
  • ajax_demo:GET POST发送数据

    GET,通过url发送数据

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script language="JavaScript">
    var xmlHttp; function login() { if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } var name = document.getElementById("name").value; var password = document.getElementById("password").value; var url = "/Crawler/Login?name="+name+"&password="+password; xmlHttp.open("GET", url, true); xmlHttp.send(null); } </script> </head> <body> 用户名:<input type="text" name="name" id="name"/></br> 密码:<input type="password" name="password" id="password"/></br> <button type="button" onclick="login();">登陆</button> </body> </html>

    POST:通过XMLHttpRequest.send(),发送xml格式数据,需在header中设置Content-Type:text/xml

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script language="JavaScript">
    var xmlHttp;
    function login() { if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } alert("send ok") var name = document.getElementById("name").value; var password = document.getElementById("password").value; var xmlString = "<profile>" + " <name>" + escape(name) + "</name>" + " <password>" + escape(password) + "</password>" + "</profile>"; var url = "/Crawler/Login"; xmlHttp.open("POST", url, true); xmlHttp.setRequestHeader("Content-Type", "text/xml"); xmlHttp.send(xmlString);//发送数据 } </script> </head> <body> 用户名:<input type="text" name="name" id="name"/></br> 密码:<input type="password" name="password" id="password"/></br> <button type="button" onclick="login();">登陆</button> </body> </html>
  • 相关阅读:
    给大家介绍几个网站学习前端和服务器语言的网站吧,一定有合适你的
    centos用yum安装软件提示:另外一个程序锁定了 yum;等待它退出
    模仿小猎CMS首页功能展示的JS效果
    在centos下安装Node.js 开发环境搭建
    discuz在IIS,apache中分别怎么设置伪静态
    CentOS系统下各文件夹的作用
    centos上网设置
    php微信公众平台开发获取access_token,用CURL出现certificate verify failed错误的解决方法
    12.9 NIO
    12.8 Java 9改进的对象序列化
  • 原文地址:https://www.cnblogs.com/yunwuzhan/p/5846692.html
Copyright © 2011-2022 走看看