在php文件的最前面设置header函数

在php文件的最前面设置header函数的内容,即在<!doctype html>的前面设置header()函数

1、header("Content-Type: text/html;charset=utf-8"); 

     这句话的意思是,设置页面的内容为html;’编码为utf8(设置编码为utf8的原因是,防止在php文件里面有汉字输出的时候,出现编         码乱码的现象出现)


2、header("Access-Control-Allow-Origin: http://www.funwall.cn");  设置跨域请求的源

      Access-Control-Allow-Origin这个标志用来设置跨域请求

     Access-Control-Allow-Origin:*;这里表示允许任何域的请求

     Access-Control-Allow-Origin:http://www.funwall.cn;这里用的是具体的url,即表示允许来自该域的访问

    同时在ajax请求里面设置属性crossDomain: true,即允许跨域

3、  header('Access-Control-Allow-Credentials: true');发送Cookie的设置认证属性

   属性withCredentials(证书):默认情况下,CORS不会发送Cookie和HTTP的认证信息,如果要把Cookie发送到服务器,一方     面要服务器同意,设置属性withCredential,即header('Access-Control-Allow-Credentials: true')。

   同时必须在Ajax请求中打开属性withCredentials,即xhrFields: { withCredentials: true }

   注意:如果发送Cookie,Access-Control-Allow-Origin就不能设置为*,必须指定明确的、与请求网页一致的域名

4、header('Access-Control-Allow-Headers:x-requested-with,content-type'); 响应头设置

5、 header("location:http://www.funwall.cn/banch/front/index.html);重定向URL,即页面跳转到

      http://www.funwall.cn/banch/front/index.html

前端的ajax请求的代码封装成函数:

function request(url, data, success_callback,error_callback) {
    console.log("url:"+url);
    var xhr = $.ajax({
        //提交数据的类型 POST GET
        type: "POST",
        //提交的网址
        url: url,
        //提交的数据
        data: data,
        // 设置超时的时间20s
        timeout:20000,
        //返回数据的格式
        datatype: "json", //"xml", "html", "script", "json", "jsonp", "text".
        xhrFields: {
            withCredentials: true   //设置发送Cookie
        },
        crossDomain: true,        //设置跨域请求
        //在请求之前调用的函数
        beforeSend: function () {

        },
        //成功返回之后调用的函数             
        success: function (response) {
            handleResponse(response, success_callback,error_callback);
        },
        //调用执行后调用的函数
        complete: function (XMLHttpRequest, textStatus) {
            if(textStatus == 'timeout'){
                if (error_callback != null && error_callback != "") {            
                    error_callback();
                };
            }
            $('#loding').css("display","none");
        },
        //调用出错执行的函数
        error: function () {
            //请求出错处理
            console.log("error");
        }
    });
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值