您的位置:首页 > Web前端 > JQuery

jquery ajax post 跨域访问带cookie

2016-08-17 20:57 447 查看
js:

//显示用户信息
$.ajax("http://ltest.com:2020/test/",{

            //提交数据的类型 POST GET

            type:"POST",

            async:false,

            data:{ 'username': username.value, 'password': userpassw.value },

            crossDomain:true,

            datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".

            xhrFields: {  withCredentials: true  },

            //成功返回之后调用的函数             

            success:function(data,status,xhr){

            if(data["state"]==1)

                {

                alert("ok");

                }else{

                alert("密码或用户名错误");

                }

            }

         });

         

服务端要加头:

Access-Control-Allow-Origin 不能为“*“,当withCredentials为真是

resp['Access-Control-Allow-Origin'] = req.META.get("HTTP_ORIGIN")

“true”不要写成“True”了,浏览器不认

resp["Access-Control-Allow-Credentials"] = "true"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: