Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript expressjs-从外部html获取路由数据_Javascript_Ajax_Node.js_Express - Fatal编程技术网

Javascript expressjs-从外部html获取路由数据

Javascript expressjs-从外部html获取路由数据,javascript,ajax,node.js,express,Javascript,Ajax,Node.js,Express,我在使用express/node时遇到问题。我创建了一个名为“/搜索”的路由,它运行良好,因为我已经对它进行了测试。我还有一个html文件,它不是express项目的一部分;它不位于“公共”或“视图”文件夹中。我正在尝试从此文件访问“/搜索”路径。我在cmd中看到请求已经完成,但是我无法从我正在执行的ajax中检索数据 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.c

我在使用express/node时遇到问题。我创建了一个名为“/搜索”的路由,它运行良好,因为我已经对它进行了测试。我还有一个html文件,它不是express项目的一部分;它不位于“公共”或“视图”文件夹中。我正在尝试从此文件访问“/搜索”路径。我在cmd中看到请求已经完成,但是我无法从我正在执行的ajax中检索数据

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
</head>    
<body>
<p id="container">Hello</p>    
<script>
      $(document).ready(function(){
        $("#container").click(function(){
            $.get('http://localhost:3000/searching',function(data){
                 $("#container").html(data);      
            });  });  });
</script>    
</body>     
</html>

我希望在单击此按钮时看到“Hello”文本变为“result”,但它不起作用。有什么建议吗?

也从3000端口发送page吗?如果不是,则是跨域请求,您将在浏览器控制台中看到警告。此外,您还可以在浏览器开发工具网络中查看实际请求以获取更多线索,还应该为sameNo添加一个ajax错误处理程序,html正在从文件夹中打开“file:///C:/...OK.. 出于安全原因,无法从
文件://
协议发出ajax请求。打开服务器上的页面。学习使用浏览器开发工具(F12)所有这一切都可以从这里解决。我从另一个端口提供了html文件。我看到请求是通过cmd完成的。对于F12,我看到这个错误:XMLHttpRequest无法加载。请求的资源上没有“Access Control Allow Origin”头。因此不允许访问Origin“”。因此,请通过在google中粘贴来研究该错误。您需要从ajax调用的同一台服务器提供服务,或者在data server上实现CORS也从端口3000提供服务吗?如果不是,这是一个跨域请求,您将在浏览器控制台中看到警告。此外,您可以在浏览器开发工具网络中查看实际请求以获取更多线索,还应该为sameNo添加一个ajax错误处理程序,正在从文件夹中打开html“file:///C:/...OK.. 出于安全原因,无法从
文件://
协议发出ajax请求。在服务器上打开页面。学习使用浏览器开发工具(F12),所有这些都可以从中解决。我从另一个端口提供html文件。我看到请求是通过cmd完成的。对于F12,我看到了这个错误:XMLHttpRequest无法加载。请求的资源上不存在“Access Control Allow Origin”标头。因此不允许访问源代码“”。请通过在google中粘贴来研究该错误。您要么需要从ajax调用的同一服务器提供服务,要么在数据服务器上实现CORS
app.get('/searching', function(req, res){
    res.send("result");
});