首页 / AJAX / ajax的post请求方式
ajax的post请求方式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ajax的post请求方式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2558字,纯文字阅读大概需要4分钟。
内容图文
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>This is my JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
div{
border: 1px solid red;
width: 400px;
height: 300px;
}
</style>
</head>
<body>
<input type="button" value="Ajax" onclick="testAjax()">
<div id="msg"></div>
</body>
<script type="text/javascript">
function testAjax(){
//document.getElementById("msg").innerHTML = "加载中...";
//id;
var request;
//创建 request对象
if(window.XMLHttpRequest){ //兼容性
request = new XMLHttpRequest();
}else if(window.ActiveXObject){ //针对IE
request = new ActiveXObject("Msxml2.XMLHTTP");
}
//写监听 去check request的状态
request.onreadystatechange = function(){
//
//console.log(request.readyState);
if(request.readyState == 4){
//得到 后台写出的数据
//当加载成功以后
if(request.status == 200){
//得到的的是一个json字符串
var data = request.responseText;
//将json字符串转换为json对象
eval("data = "+data);
document.getElementById("msg").innerHTML = data.password;
}else if(request.status == 404){
document.getElementById("msg").innerHTML = "资源没有找到";
}else if(request.status == 500){
document.getElementById("msg").innerHTML = "服务器错误";
}
}else{
document.getElementById("msg").innerHTML = "<img src=\"images/loading.gif\" />";
}
};
//打开请求
//如果在 地址栏上面 的参数 的值中包含 # 应该将其编码 然后在传递
request.open("post", "ajax/ajaxController?name="+encodeURIComponent("lisi#123")+"&d="+new Date().getTime());
//encodeURIComponent(uriComponent)
//发送数据
//如果没有数据 则写null 不然 其他浏览器可能会报错
//如果在 send方法中传参数 则一定要设置 请求头信息中的 Content-Type 为 application/x-www-form-urlencoded
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//如果是 post传参 他和 参数的组织方式 是有关系的
request.send("username=lisi#123&age=123");
}
</script>
</html>
原文:http://www.cnblogs.com/hwgok/p/5845068.html
内容总结
以上是互联网集市为您收集整理的ajax的post请求方式全部内容,希望文章能够帮你解决ajax的post请求方式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。