它是带有 NextJS 和 Go 服务器的全栈 Web 应用程序。由于 NextJS 的限制,我必须首先对 NextJS 无服务器函数进行 api 调用。然后,从该无服务器函数将 http post 请求发送到 Go 后端。
在开发中一切都运行良好。但是在部署之后,即使“GET”请求有效,所有“POST”、“PUT”和“DELETE”方法都没有发送到服务器。客户端能够获取数据。
我假设服务器工作正常。它在开发过程中按预期工作,甚至在部署之后,http 请求与 POST MAN 一起工作得很好。
有趣的是,在部署之后,POST、PUT 和 DELETE 请求从未发送到服务器,除了 GET 请求
我还在 Go 服务器中设置了 CORS
api/clinic
async function handleAdd(): Promise<void> {
try {
console.log("adding", clinic)
const res = await axios.post('/api/clinic', clinic, {
headers: {
'Content-Type': 'application/json',
},
withCredentials: true
})
const data = await res.data
console.log("successfully add clinic", data)
// window.location.reload()
} catch (error) {
console.error(error);
}
}
if (req.method === 'POST') {
try {
const response = await axios.post(`${process.env.NEXT_PUBLIC_URL}/admin/create-clinic`, clinic, {
headers: {
'Content-Type': 'application/json',
},
withCredentials: true
})
const data = await response.data
return res.end(JSON.stringify(data))
} catch (err) {
console.log(err)
return res.end(JSON.stringify({"error": err.message }))
}
}
我不明白为什么请求标头设置为 GET,即使在代码中我确定它是 POST 请求
[Symbol(kOutHeaders)]: [Object: null prototype] {
accept: [Array],
'user-agent': [Array],
'accept-encoding': [Array],
host: [Array]
},
[Symbol(errored)]: null,
[Symbol(kUniqueHeaders)]: null
},
response: {
status: 405,
statusText: 'Method Not Allowed',
headers: AxiosHeaders {
date: 'Mon, 03 Apr 2023 06:20:36 GMT',
'content-length': '0',
server: 'railway',
connection: 'close'
},
config: {
transitional: [Object],
adapter: [Array],
transformRequest: [Array],
transformResponse: [Array],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
env: [Object],
validateStatus: [Function: validateStatus],
headers: [AxiosHeaders],
withCredentials: true,
method: 'post',
url: 'https://cnaserver-production.up.railway.app//admin/create-clinic',
data: '{"id":0,"name":"CNA 3","location":"Pyin Oo Lwin Main Road"}'
},
request: <ref *1> ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
maxRequestsOnConnectionReached: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
strictContentLength: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
_closed: false,
socket: [TLSSocket],
_header: 'GET /admin/create-clinic HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'User-Agent: axios/1.3.4\r\n' +
'Accept-Encoding: gzip, compress, deflate, br\r\n' +
'Host: cnaserver-production.up.railway.app\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: nop],
agent: [Agent],
socketPath: undefined,
method: 'GET',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: '/admin/create-clinic',
_ended: true,
res: [IncomingMessage],
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'cnaserver-production.up.railway.app',
protocol: 'https:',
_redirectable: [Writable],
[Symbol(kCapture)]: false,
[Symbol(kBytesWritten)]: 0,
[Symbol(kEndCalled)]: true,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype],
[Symbol(errored)]: null,
[Symbol(kUniqueHeaders)]: null
},
data: ''
}
}
我期待在成功的 http 请求后响应中有一个 JSON 对象。但是请求从未发送到服务器。