jquery设置context type设置
jquery ajax contentType设置
默认get方法没有contentType,post方法的contentType为:application/x-www-form-urlencoded; charset=UTF-8
$.ajax({
type: 'post',
url:'/contentType',
contentType:'application/x-www-form-urlencoded',
data:{
username:'admin',
password:'123123'
},
dataType:'json',
success:function (data) {
}
})
jquery访问servlet并返回数据到页面的方法
假设:
1、你的页面在Web-Root下,内容为:
,所用编码为utf-82、你的servlet为: HelloWorldServlet.java 映射路径为 servlet/helloWorldServlet 步骤: 1、引入jquery-1.6.4.min.js 2、编写id为userName的输入框的点击触发函数: $("#userName").keyup(function(){ $.ajax({ type: "post", url: "servlet/helloWorldServlet?userName="+$(this).val(), dataType: "json", success: function(data){ $("#showMsg").html(data.msg);//修改id为showMsg标签的html }, error: function(){ alert("请求出错"); } }) })
3、后台处理接收到的内容: request.setCharactorEncoding("utf-8"); String userName = request.getParameter("userName"); response.setCharactorEncoding("utf-8"); PringWriter out = response.getWriter(); out.print("{"msg":"你好~~"+userName+"!"}"); 注意事项: 1、这里的编码统一为utf-8 2、请求路径servlet/helloWorldServlet为相对路径,因此你的页面必须在项目的Web-Root下(也就是默认的web文件夹下,名字可能因项目配置不同而改变) 3、没了,记得给分哦,打字很辛苦的~
jqueryajax怎么通过header传递参数
/ 这个是全局的,所有的ajax请求都会加上这个请求头
$(document).ajaxSend(function (event, xhr) {
xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8") ;
xhr.setRequestHeader("Authorization", "Authorization") ;
});
//局部 第一种
$('xxx').ajax({
//...
beforeSend:function(jqXHR,options){
jqXHR.setRequestHeader("Content-Type", "application/json;charset=utf-8") ;
jqXHR.setRequestHeader("Authorizationr", "Authorization") ;
}
//...
}) ;
//局部 第二种
$('xxx').ajax({
//...
headers:{
"Content-Type": "application/json;charset=utf-8",
"Authorizationr":"Authorizationr",
}
//...
}) ;
注意:修改请求头时,headers中的设置会覆盖beforeSend中的设置(意味着beforeSend先执行,所以被后面的headers覆盖)

