簡體   English   中英

JSON 解析錯誤:針對獲取 API 請求的意外標識符“數組”

[英]JSON Parse error: Unexpected identifier "Array" against Fetch API request

我正在嘗試使用 Fetch API 發出 HTTP 請求,如下所示:

const collectTargets = () =>{fetch('REQUEST URL?', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    request_type: "osm_points",
    lat: region.latitude,// coming from region state 
    lng: region.longitude,
    limit: 5,
  })
})
.then((response) => response.json())
.then((responseJson) => {
    console.log(responseJson);
})
.catch((error) => {
    console.log(error);
});
}

我收到如下錯誤。 有人可以解釋一下嗎。 我看到一些帖子說 API 必須關閉等,但這里似乎並非如此。

JSON Parse error: Unexpected identifier "Array"
* [native code]:null in parse
- node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
- node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue

注意 -: //你必須使用wp_die(); 否則返回 0 和錯誤響應。

$output = array("status"=>'success', "content"=>$content);

echo json_encode($output);

我相信在這里你的問題會解決的。

https://sknetking9.blogspot.com/2021/03/how-to-call-ajax-in-wordpress.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM