码迷,mamicode.com
首页 > Web开发 > 详细

跨域 jsonp

时间:2020-02-14 10:45:04      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:数据   headers   void   ssd   put   cep   dom   格式   writer   

$.ajax(‘http://192.168.1.107:8080/test/test.do‘;,{
data:{
username:username,
password1:password1
},

dataType:‘jsonp‘,//服务器返回json格式数据
crossDomain: true,
jsonp: ‘callback‘,
type:‘POST‘,//HTTP请求类型
timeout:10000,//超时时间设置为10秒;
success:function(data){
if(data){
alert("登录成功!");
}else{
alert("登录失败!");
}

},
error:function(xhr,type,errorThrown){
console.log(type);
//alert(xhr);
}
});
后台
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Origin","*");
response.setContentType("text/json; charset=utf-8");
response.setHeader("Access-Control-Allow-Methods","GET,POST,OPTIONS,DELETE");
response.setHeader("Access-Control-Max-Age","3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
String callback=request.getParameter("callback");
if(null==callback){
System.out.println("不是跨域请求");
}else{
System.out.println("是跨域请求");
}

PrintWriter out=response.getWriter();
String userName=request.getParameter("username");
String passWord=request.getParameter("password1");
Map<String,String> map = new HashMap<String,String>();
map.put("result", "true");
String jsonString="{\"a\":\"1\"}";
if("123456".equals(userName) && "123456".equals(passWord)){
//out.write(jsonString);
out.println(callback+"("+jsonString+")");
}else{
//out.write(jsonString);
out.println(callback+"("+jsonString+")");
}
out.flush();
out.close();
}

跨域 jsonp

标签:数据   headers   void   ssd   put   cep   dom   格式   writer   

原文地址:https://www.cnblogs.com/pengpengzhang/p/12306282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!