首页 >后端开发 >php教程 >javascript - 跨域请求的问题

javascript - 跨域请求的问题

WBOY
WBOY原创
2016-10-19 10:40:511156浏览

javascript - 跨域请求的问题

我前端的代码是这样,
后台是用php写的,

<code><?php class IBaseAction extends Action
{   
public $params = array();// 所有请求的参数
    public $return = array('returnCode' =>200, 'returnDesc' => 'success'); // 返回的基础数据
    function __construct()
    {
        $this->return['serverTime'] =time();
        Log::record('传入的参数:' . var_export($_REQUEST, true),Log::DEBUG);
        $this->params = @json_decode(base64_decode(I('post.params'),true),true);
        if (!$this->params){
            $this->return['returnCode'] =100;
            $this->return['returnDesc'] = 'sign failed!';
            $this->responseJson();
        }else if(C('KEY')!=$this->params['key']){
            $this->return['returnCode'] =100;
            $this->return['returnDesc'] = 'sign failed!';
            $this->return['returnAlert'] = '密钥不正确!';
            $this->responseJson();
        }
    }

    /*
     * 检查接口传入的方式
     */
    public function checkPublicParams($interfaces) {
        $pass = false;
        $this->return['returnCode'] = 100;
        if (!I('post.')) {
            $this->return['returnDesc'] = '请求方式错误';
        } else if (!I('post.method') || !I('post.params')) {
            $this->return['returnDesc'] = '接口参数错误';
        } else if (!array_key_exists(I('post.method'), $interfaces)) {
            $this->return['returnDesc'] = '接口名错误';
        } else {
            $this->return['returnCode'] = 200;
            $pass = true;
        }
        return $pass;
    }
    protected function responseJson()
    {
        exit(json_encode($this->return));
    }
}</code>

未改的状态是返回 认证失败
“{"returnCode":100,"returnDesc":"sign failed!","serverTime":1476799012}”
在网上也找到了修改的方法
javascript - 跨域请求的问题
但是不知如何修改

回复内容:

javascript - 跨域请求的问题

我前端的代码是这样,
后台是用php写的,

<code><?php class IBaseAction extends Action
{   
public $params = array();// 所有请求的参数
    public $return = array('returnCode' =>200, 'returnDesc' => 'success'); // 返回的基础数据
    function __construct()
    {
        $this->return['serverTime'] =time();
        Log::record('传入的参数:' . var_export($_REQUEST, true),Log::DEBUG);
        $this->params = @json_decode(base64_decode(I('post.params'),true),true);
        if (!$this->params){
            $this->return['returnCode'] =100;
            $this->return['returnDesc'] = 'sign failed!';
            $this->responseJson();
        }else if(C('KEY')!=$this->params['key']){
            $this->return['returnCode'] =100;
            $this->return['returnDesc'] = 'sign failed!';
            $this->return['returnAlert'] = '密钥不正确!';
            $this->responseJson();
        }
    }

    /*
     * 检查接口传入的方式
     */
    public function checkPublicParams($interfaces) {
        $pass = false;
        $this->return['returnCode'] = 100;
        if (!I('post.')) {
            $this->return['returnDesc'] = '请求方式错误';
        } else if (!I('post.method') || !I('post.params')) {
            $this->return['returnDesc'] = '接口参数错误';
        } else if (!array_key_exists(I('post.method'), $interfaces)) {
            $this->return['returnDesc'] = '接口名错误';
        } else {
            $this->return['returnCode'] = 200;
            $pass = true;
        }
        return $pass;
    }
    protected function responseJson()
    {
        exit(json_encode($this->return));
    }
}</code>

未改的状态是返回 认证失败
“{"returnCode":100,"returnDesc":"sign failed!","serverTime":1476799012}”
在网上也找到了修改的方法
javascript - 跨域请求的问题
但是不知如何修改

对于 OPTIONS 请求,返回的 HTTP 响应应该包含以下信息:

1.允许跨域请求的方法

<code>Access-Control-Allow-Methods: GET,POST</code>

2.允许跨域请求的自定义头部

如 AJAX 请求

<code>Access-Control-Allow-Headers: X-Requested-With</code>

3.允许跨域请求的源

<code>Access-Control-Allow-Origin: *</code>

详细可以参考:

http://www.ruanyifeng.com/blo...

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn