将数据回显到Vue时的ReadableStream



我正试图使用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);
});

相关内容

  • 没有找到相关文章

最新更新