第一次写博客这玩意写的不好请大家多多见谅,希望对大家有所帮助。关于跨域问题本人说一下自己的理解,就是你用ajax请求一个action的时候那个action不在你这个项目里面,或者请求的就是一个webservice接口的情况。其他专业的解释我就不copy了,就是这么个意思。这种情况咋办呢?第一个方法用nginx这个工具,通过这个工具。网上一搜一大堆概念的说法,这个玩意功能强大,不同的项目启动起来需要不同的端口号在一台电脑上,当这个应用启动起来的时候配置conf下面的config.cnf文件就可以转发一下,你用ajax请求8080端口的时候请求8888端口具体配置如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location /elws/ {
proxy_pass http://192.168.2.151:8080/webservicename/;
}
# rewrite ^/elws/(.*)$ /$1 break;
location / {
proxy_pass http://127.0.0.1:8888;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:8080
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
第二种使用jsonp的解决方式:这个方法我的理解就是在java端构造一个javascript的callback函数,把你要传的内容作为callback函数的参数传到前端
回调函数:
"use strict"
var address="https://localhost/adress";
function sendNetworkReq(method,params,callback){
requestUrl(address+method,method,params,callback);
console.log("callback函数执行了"+params);
}
function requestUrl(url,method,params,callback){
var k;
var str="";
for(k in params){
str+="&"+k+"="+params[k];
}
console.log("callback函数请求了"+str);
$.ajax({
//console.log(str)
type:"get",
url:url,
async:true,
dataType:"jsonp",
data:str,
jsonp:"callback",
jsonpCallback:"callBack",
success:function(e){
callback(JSON.stringify(e));
//var i=0;
//i++;
//console.log(JSON.stringify(e)+i)
//console.log(JSON.stringify(e));
},
error:function(e){
callback(JSON.stringify(e));
console.log(JSON.stringify(e))
alert("test error");
}
});
function callBack(data){
alert("回掉成功"+data);
}
}
login.html:
var address="https://localhost/adress";
function sendNetworkReq(method,params,callback){
requestUrl(address+method,method,params,callback);
console.log("callback函数执行了"+params);
}
function requestUrl(url,method,params,callback){
var k;
var str="";
for(k in params){
str+="&"+k+"="+params[k];
}
console.log("callback函数请求了"+str);
$.ajax({
//console.log(str)
type:"get",
url:url,
async:true,
dataType:"jsonp",
data:str,
jsonp:"callback",
jsonpCallback:"callBack",
success:function(e){
callback(JSON.stringify(e));
//var i=0;
//i++;
//console.log(JSON.stringify(e)+i)
//console.log(JSON.stringify(e));
},
error:function(e){
callback(JSON.stringify(e));
console.log(JSON.stringify(e))
alert("test error");
}
});
function callBack(data){
alert("回掉成功"+data);
}
}
login.html:
function test(){
var username = $("#username").val();
var password = $("#password").val();
//alert(username+password);
var infor={
username:username,
password:password
}
func(infor);
}
function func(infor){
console.log(infor.username);
//var username=$("#username").val();
//var password=$("#password").val();
//alert(infor.username);
//console.log(infor);
var url="User_login";
sendNetworkReq(url,infor,function(result){
var result1= JSON.parse(result);
console.log(result1)
if(result1==1001){
$("#psinfor").css("display","block");
}else if(result1==1002){
$("#usinfor").css("display","block");
}else {
$("#loginName").text(infor.username);
$("#register").text("退出");
//saveCookie("realName",msg.localRealName);
saveCookie("senderId", result1.localUserId);// userId就是senderId
};
});
}
var username = $("#username").val();
var password = $("#password").val();
//alert(username+password);
var infor={
username:username,
password:password
}
func(infor);
}
function func(infor){
console.log(infor.username);
//var username=$("#username").val();
//var password=$("#password").val();
//alert(infor.username);
//console.log(infor);
var url="User_login";
sendNetworkReq(url,infor,function(result){
var result1= JSON.parse(result);
console.log(result1)
if(result1==1001){
$("#psinfor").css("display","block");
}else if(result1==1002){
$("#usinfor").css("display","block");
}else {
$("#loginName").text(infor.username);
$("#register").text("退出");
//saveCookie("realName",msg.localRealName);
saveCookie("senderId", result1.localUserId);// userId就是senderId
};
});
}