鍍金池/ 問答/PHP  HTML/ ajax獲取不到php方法里curl請求的數(shù)據(jù)。直接通過url地址訪問可以獲取數(shù)

ajax獲取不到php方法里curl請求的數(shù)據(jù)。直接通過url地址訪問可以獲取數(shù)據(jù)。

通過url直接訪問php方法里一個跨域請求可以得到數(shù)據(jù),但是用ajax請求返回的結(jié)果為空。

php 代碼

    $host = "xxx.xxxx.cn/Home/Index/index"; 
    // $header[] = "Content-type: text/xml;charset=UTF-8";
    // $ch = curl_init();
    $ch = curl_init($host); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($host));    //參數(shù)
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT,5);
    $arra = curl_exec($ch);
    curl_close($ch);
    $arra = json_decode($arra,true);
    foreach ($recommend as $value) {    
        foreach ($arra as $v) {
            if ($value['id'] == $v['shop_id']) {
                $rmd[]=array_merge_recursive($value,$v);
            }
        }
    }
    if (count($rmd) < count($recommend)) {
        $diff = array_diff_key($recommend,$rmd);
        foreach ($diff as $val) {
            $pageview['pageview'] = 0;
            $aa[] = array_merge_recursive($value,$pageview);
        }
    }
    $recommends =array_merge_recursive($aa,$rmd);
    // var_dump($recommends);   
    return $this->ajaxReturn($recommends , 1, 1);

ajax
$.ajax({

            type: "POST",
            dataType: "json",
            url: "{:U('Shop/Site/GetRecommend')}",
            data: null,
            success: function(result) {
            //這里得到的結(jié)果為空
                console.log(result) 
        });

被折磨一天了,完全不清楚是什么原因。

回答
編輯回答
我不懂

ajax 請求接口,要獲取返回值,一定要加

    async:false,
    type: "post",
    dataType: "json",
2018年2月11日 01:27
編輯回答
懶豬

可能是數(shù)據(jù)類型不對,返回到ajax 錯誤信息里面去了。 error:function(){} 看看

2017年1月23日 03:49
編輯回答
厭遇

打開瀏覽器開發(fā)者工具看下ajax的網(wǎng)絡(luò)請求跟返回值不就知道啥情況了。

2017年12月28日 17:33
編輯回答
淚染裳

查看一下 服務(wù)端對 url 的支持是哪種類型的, 看你的意思, 直接訪問url是可以的, getpost 了解一下.

2017年11月22日 01:42
編輯回答
吢涼

看看你的ajaxReturn方法

2017年9月6日 05:41