Node.js 为什么Ajax调用在heroku部署中给出连接拒绝错误,但在本地主机上运行良好?

Node.js 为什么Ajax调用在heroku部署中给出连接拒绝错误,但在本地主机上运行良好?,node.js,ajax,express,heroku,Node.js,Ajax,Express,Heroku,我已经用mongodb构建了一个节点应用程序,当我用localhost运行它时,所有ajax请求都会发出,json对象也会成功返回,但是当我将应用程序部署到heroku时,所有ajax调用都会出错,例如: 未查看的帖子:98获取http://localhost:3000/approvepostbyadmin/be3 网络::错误连接被拒绝 此ajax调用通过单击按钮进行: <button id="approve" getpost= "be3"

我已经用mongodb构建了一个节点应用程序,当我用localhost运行它时,所有ajax请求都会发出,json对象也会成功返回,但是当我将应用程序部署到heroku时,所有ajax调用都会出错,例如:

未查看的帖子:98获取http://localhost:3000/approvepostbyadmin/be3 网络::错误连接被拒绝

此ajax调用通过单击按钮进行:

  <button id="approve" getpost= "be3"  role="admin" onclick="approveFunc(this)">approve this post and send to respective posts page</button>
批准此帖子并发送到相应的帖子页面
下面是脚本:

<script>
      
 
  var httpRequest;
  var status = document.querySelector('#reviewstatus');
  var test; 
  function approveFunc(e){
      curPostSlug = e.getAttribute('getpost');           
      console.log(curPostSlug);
      httpRequest = new XMLHttpRequest();
      if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
      }
      httpRequest.onreadystatechange = approveFuncRequest;
      httpRequest.open('GET', 'http://localhost:3000/approvepostbyadmin/'+ curPostSlug);
      httpRequest.send();
    }
    function approveFuncRequest(){
      if (httpRequest.readyState === XMLHttpRequest.DONE) {
        if (httpRequest.status === 200 ) {
          var resJson = JSON.parse(httpRequest.responseText)
          console.log(resJson.reviewed)
          status.innerHTML = "resJson.reviewed"
        } else {
          alert('There was a problem with the request.');
        }
      } else {
        console.log("not ready")
      }
    }
<script>

var-httpRequest;
var status=document.querySelector(“#reviewstatus”);
var检验;
函数approveFunc(e){
curpostslaug=e.getAttribute('getpost');
控制台日志(curpostslaug);
httpRequest=新的XMLHttpRequest();
如果(!httpRequest){
警报(“放弃:(无法创建XMLHTTP实例”);
返回false;
}
httpRequest.onreadystatechange=approveFuncRequest;
httpRequest.open('GET','http://localhost:3000/approvepostbyadmin/“+curPostSlug);
httpRequest.send();
}
函数approveFuncRequest(){
if(httpRequest.readyState==XMLHttpRequest.DONE){
if(httpRequest.status==200){
var resJson=JSON.parse(httpRequest.responseText)
log(resJson.revieved)
status.innerHTML=“resJson.revieved”
}否则{
警报(“请求有问题”);
}
}否则{
console.log(“未准备就绪”)
}
}
这只是一个地方,它为我使用过的所有页面上的所有ajax请求提供相同的eroor。 浏览器在httpRequest.send()处显示错误

原因可能是什么,我已经正确设置了所有的env变量,是因为我在请求URL中使用了localhost:3000,如果是,我应该在那里使用什么。
如何让它工作,如果有任何帮助,我们将不胜感激。

您不能在那里使用localhost,您需要为端点使用有效的主机名,我必须使用哪个主机名。我可以从哪里找到它?@michelemI不知道您必须在那里做什么,它可能是您为项目选择的Heroku主机名,但只有您知道该调用的位置p点。。。