javascript - 跨域請求的問題

WBOY
發布: 2016-10-19 10:40:51
原創
1143 人瀏覽過

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...

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板