首頁  >  問答  >  主體

使用 formdata 模組使用 Node js 和 axios 將檔案發佈到 php 伺服器時出現 503 錯誤

我正在嘗試將檔案從我的 Node js 應用程式傳送到託管 opencart 應用程式的 PHP 伺服器。 我正在使用 formdata 和 axios 模組來發出請求並上傳檔案。

我的問題是我收到此錯誤 錯誤:請求失敗,狀態代碼 503

如何解決?

這是我在 Node.js 中的程式碼:

let form = new FormData();
form.append("file", fs.createReadStream(path.resolve(zipFilePath)), path.basename(zipFilePath));

            try {
                let response = await axios.post(endpoint, form, {
                    headers: {
                        ...form.getHeaders(),
                    },
                });

                const result = response.data;
                if (result && result.status === "success") {
                    fs.unlinkSync(zipFilePath);
                }
            } catch (e) {
                console.log(e.toString());
            }

和 php 程式碼(控制器中的函數):

public function upload() {
        header('Access-Control-Allow-Origin: *');
        
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            $this->response->setOutput(json_encode([]));
        } else {
            // process the file posted
        }
    }

P粉502608799P粉502608799210 天前318

全部回覆(1)我來回復

  • P粉436052364

    P粉4360523642024-02-27 09:01:35

    問題已解決。

    問題是opencart應用程式在後端設定為維護模式,但前端仍然正常工作,所以之前沒有註意到。

    回覆
    0
  • 取消回覆