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>
  • 相关阅读:
    python迭代器与iter()函数实例教程
    手动安装python后,交互模式下退格键乱码
    find参数exec、管道符|、xargs的区别
    比较好的网址收集
    sed小知识总结
    irc操作小记
    irssi忽略退出,加入消息
    Web自动化简介
    android&ios区别
    移动自动化应用展望
  • 原文地址:https://www.cnblogs.com/yunwuzhan/p/5846692.html
Copyright © 2011-2022 走看看