[英]404 /server Not Found - Nodejs Ajax
i am trying to send data from app.js to nodejs server.js but AJAX is giving error POST http://localhost:9615/server 404 (Not Found)
我试图将数据从app.js发送到nodejs server.js,但AJAX给出了错误POST http://localhost:9615/server 404 (Not Found)
When I open http://localhost:9615/server
on browser it work fine and show desired output that is callback('{"msg": "OK"}')
当我在浏览器上打开http://localhost:9615/server
,它可以正常工作并显示所需的callback('{"msg": "OK"}')
输出callback('{"msg": "OK"}')
Here is app.js code 这是app.js代码
I have also tried url: 'server' and '/server' 我也尝试过网址:“服务器”和“ /服务器”
$.ajax({
url: 'http://localhost:9615/server',
// dataType: "jsonp",
data: '{"idToken": '+idToken+'}',
type: 'POST',
jsonpCallback: 'callback',
success: function (data) {
var ret = jQuery.parseJSON(data);
console.log('Success: '+ret.msg);
},
error: function (xhr, status, error) {
console.log('Error: ' + error.message);
},
});
Here is server.js code 这是server.js代码
app.get('/server', function(req,res, next){
console.log('Request received: ');
util.log(util.inspect(req))
util.log('Request recieved: \nmethod: ' + req.method + '\nurl: ' + req.url) // this line logs just the method and url
res.writeHead(200, { 'Content-Type': 'text/plain' });
req.on('data', function (chunk) {
console.log('GOT DATA!');
});
res.end('callback(\'{\"msg\": \"OK\"}\')');
});
Browser Screenshot: 浏览器截图:
Console Screenshot: 控制台截图:
You are trying to POST something on a GET endpoint. 您正在尝试在GET端点上发布内容。
Change to post 更改为发布
app.post('/server', function(req,res, next){
console.log('Request received: ');
util.log(util.inspect(req))
util.log('Request recieved: \nmethod: ' + req.method + '\nurl: ' + req.url) // this line logs just the method and url
res.writeHead(200, { 'Content-Type': 'text/plain' });
req.on('data', function (chunk) {
console.log('GOT DATA!');
});
res.end('callback(\'{\"msg\": \"OK\"}\')');
});
because when you do ' http://localhost:9615/server ' in browser its a get request 因为当您在浏览器中执行“ http:// localhost:9615 / server ”时,它会获得请求
& The one you are trying in code is post request. &您在代码中尝试的是发布请求。
Change either one as per your use .! 根据您的使用更改一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.