gpt4 book ai didi

php - 我想通过 HTTP 请求将数据从 Node js 文件发送到 PHP 文件(如 AJAX),但它不起作用,并且我没有收到另一方的响应

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:37 26 4
gpt4 key购买 nike

app.get('/curl', function(request, response) {


var data = querystring.stringify({
name : "bob"
});

var options = {
host: 'localhost',
port: 80,
path: 'http://matricore.com/curl_data/index.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}

};

var req = http.request(options, function(response) {
console.log('STATUS: ' + response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

req.write('data\n');
req.write('data\n');
response.send(data);
req.end();


});

我一直在尝试将一些静态数据发布到某些网址,但它不起作用,需要帮助。

最佳答案

您的 http 请求有问题。选项中的 Host 应该是域名,path 是端点到域名的相对路径。

app.get('/curl', function(request, response) {
var data = querystring.stringify({
name : "bob"
});

var options = {
host: 'matricore.com',
port: 80,
path: '/curl_data/index.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};

var req = http.request(options, function(response) {
console.log('STATUS: ' + response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

req.write('data\n');
req.write('data\n');
response.send(data);
req.end();
});

更新3

这肯定有效

var request = require('request');

app.get('/curl', function (req, res) {
request({
method: 'POST',
url:'http://matricore.com/curl_data/index.php',
form: {name:'bob'}
},function(err,httpResponse,body){
res.send(body);
});
});

enter image description here

关于php - 我想通过 HTTP 请求将数据从 Node js 文件发送到 PHP 文件(如 AJAX),但它不起作用,并且我没有收到另一方的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36308202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com