当我将数据回显到 Vue 时的 ReadableStream

ReadableStream when I echo data to Vue

我正在尝试使用 Vue (JS) 中的 fetch 方法调用 CI3 函数。 我的获取代码如下所示:

fetch("https://YOUR_LINK", requestOptions)
                    .then(function(response) {
                        return response.body;
                    })
                    .then(function(data) {
                        console.log(data);
                    });

我的 CI3 控制器功能如下:

 $ret = new stdClass();

        $ret->total_count           = 0;
        $ret->incomplete_results    = false;
        $ret->items                 = array();

        $jsonArray = json_decode(file_get_contents('php://input'),true);
        $termen = $jsonArray['q'];

        $url = 'PRIVATE_LINKq='.$termen;

        $ch = curl_init();
        $url = "http://PRIVATE_LINK_HERE?q=" . $termen . "&format=json";
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POST, 1);

        $headers = [
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Encoding: gzip, deflate',
            'Accept-Language: en-US,en;q=0.5',
            'Cache-Control: no-cache',
            'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
            'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0'
        ];

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $res = curl_exec ($ch);
        curl_close ($ch);


        $adrese = json_decode($res);


        foreach($adrese as $a) {
            $ret->total_count+= 1;
            $item = new stdClass();
      
            $item->Id = $a->place_id;
            $item->Nume =  $a->display_name;
            $item->Long = $a->lat;
            $item->Lat = $a->lon;
            $ret->items[] = $item;
        }

       echo json_encode($ret);

问题是我无法在前端获取 ret 的内容。我什么都试过了,但每次我得到

ReadableStream {locked: false}

如何在前端获取响应作为对象?

提前致谢,新年快乐!

如果您打算从端点获取 JSON,则需要将正文获取为 json,试试这个:

fetch("https://YOUR_LINK", requestOptions)
  .then(function(response) {
    return response.text();
  })
  .then(function(data) {
    console.log(data);
  });