Ajax cannot get the data requested by curl in the php method. Data can be obtained directly through url address access.

data can be obtained by directly accessing a cross-domain request in the php method through url, but the result returned by the ajax request is empty.

php Code

    $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));    //
    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) {
            //
                console.log(result) 
        });

has been tortured all day, and I have no idea why.

Apr.07,2021

Open the developer tool and look at the web search and return values of ajax to see what's going on.


look at your ajaxReturn method


ajax request API. To get the return value, you must add

.
    async:false,
    type: "post",
    dataType: "json",

check what type of server supports url , see what you mean, visit url directly, get and post find out.


may be the wrong data type and returned to the ajax error message. Error:function () {} look at

Menu