Javascript 404/找不到服务器-Nodejs Ajax
我试图将数据从app.js发送到nodejs server.js,但AJAX给出了错误Javascript 404/找不到服务器-Nodejs Ajax,javascript,jquery,node.js,ajax,Javascript,Jquery,Node.js,Ajax,我试图将数据从app.js发送到nodejs server.js,但AJAX给出了错误POSThttp://localhost:9615/server 404(未找到) 当我打开http://localhost:9615/server在浏览器上,它工作正常,并显示所需的输出,即回调(“{”msg:“OK”}”) 下面是app.js的代码 app.get('/server', function(req,res, next){ console.log('Request received: '
POSThttp://localhost:9615/server 404(未找到)
当我打开http://localhost:9615/server
在浏览器上,它工作正常,并显示所需的输出,即回调(“{”msg:“OK”}”)
下面是app.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\"}\')');
});
我还尝试了url:'server'和'/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);
},
});
下面是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\"}\')');
});
浏览器屏幕截图:
控制台屏幕截图:
因为当你这样做的时候
浏览器中的“”是get请求
&您正在代码中尝试的是post请求
根据您的使用情况更改其中一个 因为当你这样做的时候
浏览器中的“”是get请求
&您正在代码中尝试的是post请求
根据您的使用情况更改其中一个 您正试图在GET端点上发布内容 换岗
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\"}\')');
});
您正试图在GET端点上发布某些内容 换岗
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\"}\')');
});