我想用HTTP请求将数据从节点js文件发送到PHP文件,比如AJAX,但它不起作用,而且我没有从另一方得到响应
我一直试图将一些静态数据发布到某个url,但它不起作用,需要帮助。您的http请求有问题<选项中的code>Host应该是域名,我想用HTTP请求将数据从节点js文件发送到PHP文件,比如AJAX,但它不起作用,而且我没有从另一方得到响应,php,node.js,Php,Node.js,我一直试图将一些静态数据发布到某个url,但它不起作用,需要帮助。您的http请求有问题Host应该是域名,path是端点到域名的相对路径 app.get('/curl', function(request, response) { var data = querystring.stringify({ name : "bob" }); var options = { host: 'localhost', port:
path
是端点到域名的相对路径
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();
});
更新3
这肯定有效
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();
});
您的http请求有问题<选项中的code>Host应该是域名,
path
是端点到域名的相对路径
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();
});
更新3
这肯定有效
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();
});
您的
http.request
有问题,您的http.request
有问题,您的回复有很多问题。我尝试过,但仍然无效@mukesh sharmai无法导航到其他文件(url)您共享的代码,将数据发布到matricore.com/curl\u data/index.php
,成功发布后,它在控制台中打印主体。因此,请尝试在应用程序中获取'/curl'
,并检查是否观察到上述行为。app.GET('/',function(request,response)…通过这样做,它会在本地主机上打开索引页,并在控制台中打印正文和数据。它还在本地主机页上打印数据,但不会将其发布到我想要的url…:(那么,你是说它没有将数据发布到matricore.com/curl_data/index.php
。对吗?谢谢你的回复。我试过了,但仍然不起作用@mukesh sharmai我无法导航到另一个文件(url)您共享的代码,将数据发布到matricore.com/curl\u data/index.php
,成功发布后,它将在控制台中打印正文。因此,尝试在应用程序中获取
“/curl”
,并检查是否观察到上述行为。app.GET(“/”,函数(请求,响应)…通过这样做,它在localhost上打开索引页,并在控制台中打印正文和数据。它还在localhost页上打印数据,但没有将其发布到我想要的url…:(因此,您是说它没有将数据发布到matricore.com/curl\u data/index.php
。对吗?