Ajax不在同一端口上向本地主机发送请求 [英] Ajax not sending request to localhost on same port

查看:113
本文介绍了Ajax不在同一端口上向本地主机发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在 localhost:3000 上的节点(快速)服务器.另一方面,我有一个HTML( client.html )文件,该文件由服务器本身提供,位于路由/client 上(该文件作为响应发送).

I have a node (express) server running at localhost:3000. On another hand, I have an HTML (client.html) file which is served by the server itself, on the route /client (the file is sent as the response).

我在这里的目标是使用 client.html 页面上的AJAX调用(由同一服务器提供服务)访问另一条路由,即/test .但是,从帖子中可以很明显地看出来,它是行不通的.

My goal here is to access another route, namely /test using an AJAX call on the client.html page (WHICH IS SERVED BY THE SAME SERVER). However, as it's quite obvious from the post, it isn't working.

不是重复的:到目前为止,我已经尝试了以下SO帖子中给出的解决方案,但没有成功:

Not a duplicate: I have tried the solutions given in the following SO posts, so far, without success:

来自本地文件系统(Windows file:///)的jQuery Ajax请求-尽管
&公牛;尝试时,ajax调用不起作用将数据从本地主机发送到本地主机:8000
&公牛;为什么不将CORS标头添加到OPTIONS路由允许浏览器访问我的API?

这是我的源代码.

server.js

const express = require("express")
const bodyParser = require("body-parser")

var app = express()
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json())
...
app.get('/', (req, res)=>{
    res.setHeader('Access-Control-Allow-Origin', '*')
    res.send('This server does not support GET requests.')
})

app.post('/test', (req, res)=>{
    res.setHeader('Access-Control-Allow-Origin', '*')
    res.send("response from /test")
})

var port = 3000
var server = app.listen(port)

client.html

<html>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
    <script>
        function init(){
            $.ajax({
                url: 'http://127.0.0.1:3000/test',
                method: 'POST',
                contentType: 'application/x-www-form-urlencoded'
            }).done((data)=>{
                alert(data);
            });
        }
    </script>
    <body onload="javascript:init()">
    </body>
</html>

我尝试同时发送 GET POST 请求,但它们均无效.

I have tried sending both a GET and a POST request but none of them worked.

我以前已经这样做过,只是没有使用 nodejs .当我使用 requests 库从 Python 脚本发送请求时,收到了响应,因此服务器似乎可以正常工作.

I have done this before, only not with nodejs. When I send a request from a Python script, using the requests library, the response is received, so the server seems to work just fine.

仅供参考,我在Windows上.

FYI, I'm on Windows.

问题出在哪里和/或什么地方?

Where and/or what is the issue in?

推荐答案

我尝试使用您的客户端代码(在我添加了丢失的jquery导入之后),以下方法似乎运行良好(我没有使用express):

I tried with your client code (after I added missing jquery import) following which seems to work well (I havent use express):

const server = http.createServer(function (req, res) {
    console.log("logging stuff")
    res.setHeader('Access-Control-Allow-Origin', '*')
    res.write('This server does not support GET requests.')
    res.end();
});

我建议在终端上运行curl,以查看您实际返回的标头- curl -I localhost:3000 .

I would suggest to run curl on your terminal to see what headers you actually get back - curl -I localhost:3000.

这篇关于Ajax不在同一端口上向本地主机发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆