Ajax响应不会返回数据

时间:2018-12-05 05:34:49

标签: php ajax codeigniter-3

我找不到为什么我的Ajax无法返回任何数据的原因。如果我使用print_r($ data),将显示我的结果,但是如果我只返回$ data,它将不显示结果,我的代码如下。 (我正在使用codeigniter)

$.ajax({   
            method :'GET',
            url:baseUrl+'page/getToken/'+getUrlParameter('code'),
          dataType: 'json',
            success:function(data){
                alert(data); 
            },
            complete: function(){

            },
            error:function (xhr, ajaxOptions, thrownError){alert(thrownError);}
            });

页面控制器

public function getToken($CODE)
{
    return $this->EnvatoOperationsHandler->get_envato_token($CODE);
}

模型EnvatoOperationsHandler

function get_envato_token($code) {

    $api_url = 'https://api.envato.com/';
    $EnvatoSettings['envatoapi']=$this->get_data();
    $client_id = $EnvatoSettings['envatoapi'][0]['e_clientID'];
    $client_secrets_envo = $EnvatoSettings['envatoapi'][0]['e_clientSecret'];


    $redirect_uri='http://localhost/verify/settings';
    $client_secret=$client_secrets_envo;
    //private $access_token;
    //private $personal_token;

    $url = $api_url.'token';
    $params = array(
        'grant_type'    => 'authorization_code',
        'code'          => $code,
        'redirect_uri'  => $redirect_uri,
        'client_id'     => $client_id,
        'client_secret' => $client_secret,
    );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_VERBOSE, false);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
    $result = curl_exec($curl);
    curl_close($curl);
    $data = json_decode($result, true);
    //print_r($data);
    exit(json_encode($data));
}

1 个答案:

答案 0 :(得分:-1)

Beans回答正确。所以我根据他的回答修改了上面的代码,它起作用了。谢谢。