1.第一步 服务端设置响应头
header('Access-Control-Allow-Origin:*'); //支持全域名访问,不安全,部署后需要固定限制为客户端网址
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
header('Access-Control-Allow-Headers:x-requested-with,content-type'); //响应头 请按照自己需求添加。
2.第二步 ajax 请求服务器代码
$("#btn").click(function(){
var jsonData = {email:'XXX@qq.com',password:'1234343'};
$.ajax({
method:'POST',
url : JSON.stringify(jsonData),
dataType:'josn',
beforeSend:function(xhr){
xhr.setRequestHeader("client_type","DESKTOP_WEB");
}
success:function(data){
console.log(data);
}
});
});