首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用NGINX时,无法在Node.js下使用AJAX发出GET请求

使用NGINX时,无法在Node.js下使用AJAX发出GET请求
EN

Stack Overflow用户
提问于 2018-05-31 09:02:18
回答 1查看 113关注 0票数 0

尽管我已经学习并在我的本地服务器上使用Node实现了AJAX请求。我发现我在本地服务器上创建的请求在云服务器上不起作用。

当我使用AJAX向服务器(Node)请求数据时,由于URL的原因,请求无法到达服务器(或者我认为是这样)。

节点编码:

app.get("/",function(req,res){

    app.use(express.static(__dirname + '/'));

        app.get("/mypath/",function(req,res){
            console.log("please tell me that you arrive here"); //This actually never happens
            //some functions using the data in "req" and putting there in var "data"
            res.send(data);
        });
});

Javascript代码:

$.ajax({
        url: 'http://localhost:3000/mypath/',
        type: 'GET',
        data: {//some data to use on the server},
        dataType: "json",
        complete: function(data){
          //some stuff with the transformed data
        },
    });

上面的代码可以在本地服务器(我的pc)上运行。但不是在云端,我在尝试使用NGINX和Express处理服务器静态文件时遇到了一些问题,但我能够解决这个问题。

您是否认为,考虑到我提供静态文件的方式以及我在云服务器上工作的方式,当我们试图通过URL进行通信时,我应该以不同的方式使用AJAX请求吗?

控制台日志:

Console Log after using Marcos solution

编辑:来自jQuery AJAX和节点请求的代码

节点设置:

var express = require('express');
var app = express();
var request = require("request");
var db = require('mysql');
const path = require('path');
var http = require("http").Server(app);
var io = require("/usr/local/lib/node_modules/socket.io").listen(http);

http.listen(3000, 'localhost'); //At the end of everything between here and the code.

获取节点代码:

app.use(express.static(__dirname + '/'));

app.get('/reqData',function(req,res){
    //I never arrive here
    console.log("print that I am here");
                transform(req.query.selection1,req.query.selection2,function(){
                res.json(data); //data is transformed globally  
                });

});

Ajax:

function requestData(){
     $.ajax({
            url: 'http://localhost:3000/reqData',
            type: 'GET',
            data: {//Some stuff to send},
            dataType: "json",
            complete: function(data){

              do(data);
            },
            error: function(e){

              console.log(e);
            }
       });

    }

新控制台日志: Chrome Console Error

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-31 09:11:16

你的主要问题是你如何定义你的路由,除非你首先向/发出请求,否则你将无法到达/mypath/

您不应该以这种方式定义路由,每个路由都应该单独定义,而不是以嵌套的方式定义。

app.get('*', (req, res, next) => {
    // Set your cookies here, or whatever you want
    // This will happen before serving static files
    next(); // Don't forget next.
});

app.use(express.static(__dirname + '/'));

app.get('/mypath/', (req,res) => {
    console.log("please tell me that you arrive here"); //This actually never happens
    //some functions using the data in "req" and putting there in var "data"
    res.send(data); // make sure it is defined.
});

而且您的路由app.get('/')express.static(__dirname + '/')冲突,所以如果您只提供index.html文件,则应该删除它,这似乎就是您的情况。

然后,在您的$.ajax中,您应该将协议添加到URL中。

$.ajax({
    url: 'http://yourdomain.com/mypath/',
    // url: 'http://localhost:3000/mypath/',
    /* ... */
});

现在,假设您已经正确设置了所有其他内容,您将能够访问/mypath

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50615248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档