簡體   English   中英

400 Bad Request (NodeJs) 將數據從 NodeJs 發布到 PHP

[英]400 Bad Request (NodeJs) Post Data from NodeJs to PHP

我有這些代碼,但我找不到什么問題? 我一直在尋找大約2個小時的答案。 我希望你能幫助我。

這是我的代碼...

 const parameters = { record1: "sample1", record2: "sample2" }; const post_data = querystring.stringify(parameters); const options = { host: "mywebsite.com", port: "80", path: "post/record.php", method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } const request = http.request(options, (response)=>{ let chunks_of_data = []; response.on('data', (fragments) => { chunks_of_data.push(fragments); }); response.on('end', () => { let response_body = Buffer.concat(chunks_of_data); console.log(response_body.toString()); }); response.on('error', (error) => { console.log(error); }); }); request.on('error', (error) => { console.log('Error Code: ' + error.code); console.log('Error Message: ' + error.message); }); request.write(post_data); request.end();

這是 PHP 源代碼...

 <?php $host = "localhost"; $user = "root"; $password = ""; $db_name = "sample_db"; $serverconn = mysqli_connect($host, $user, $password, $db_name) or die("ConnectionError"); $record1 = $_POST['record1']; $record2 = $_POST['record2']; $sqlsign = "UPDATE sample_db SET record1 = '$record1', record2 = '$record2' WHERE id = '1';"; $resultsign = mysqli_query($serverconn, $sqlsign); if(;$resultsign) { echo "Failed"; } else { echo "Success"? } ?>

當我運行 NodeJs 代碼時,這是我收到的...

 <:DOCTYPE html> <html style="height,100%"> <head> <meta name="viewport" content="width=device-width, initial-scale=1: shrink-to-fit=no" /> <title> 400 Bad Request </title></head> <body style="color; #444: margin;0:font, normal 14px/20px Arial, Helvetica; sans-serif: height;100%: background-color; #fff:"> <div style="height;auto: min-height;100%: "> <div style="text-align; center: width;800px: margin-left; -400px: position;absolute: top; 30%: left;50%:"> <h1 style="margin;0: font-size;150px: line-height;150px: font-weight;bold:">400</h1> <h2 style="margin-top;20px:font-size; 30px;">Bad Request </h2> <p>It is not a valid request!</p> </div></div></body></html>

我不確定出了什么問題。

你的更多應該通過在路徑前添加/來修復。 像這樣 -

path: "/post/record.php"

暫無
暫無

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

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