Cordova app ajax对localhost的调用失败.:: ERR_CONNECTION_REFUSED

Eas*_*ude 1 ajax android node.js express cordova

我在localhost:3000上运行了一个node.js(express)webapp.我现在正在尝试使用cordova开发移动应用程序.

我有一个在我的快递应用程序'localhost:3000/tweets'中定义的路由,当被调用时会使用twitter API获取一些推文并将它们作为json对象发回.Everythinngs使用网络应用程序工作得非常好但是我很难通过移动应用程序进行相同的ajax调用.为了允许来自其他主机的请求,我已将此添加到我的edxpress app.js:编辑:我现在使用cors中间件:

  app.use(cors());
Run Code Online (Sandbox Code Playgroud)

在我的cordova应用程序中:

元标记:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
Run Code Online (Sandbox Code Playgroud)

config.xml中

    <?xml version='1.0' encoding='utf-8'?>
<widget id="<id...>" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>appname</name>
    <description>
        A twitter search app.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <access origin="*" />
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
    <engine name="android" spec="^6.2.3" />
    <plugin name="cordova-plugin-camera" spec="^2.4.1" />
    <plugin name="cordova-plugin-whitelist" spec="^1.3.2" />
</widget>
Run Code Online (Sandbox Code Playgroud)

在index.html文件中,我链接了body底部的'querySender.js'文件:

    <script type="text/javascript" src="js/querySender.js"></script>
Run Code Online (Sandbox Code Playgroud)

最后,'querySedner.js'的内容

$(document).ready(function(){

  $('#btn_send').on('click', function(){
    console.log("app is now sending query!");
    $.ajax({
        type: 'POST',
        url: 'http://127.0.0.1:3000/tweets',
        data: {name:"test_name",lastname:"last_name"},
        dataType:'json',
        success: function(dataR){
          var date = new Date();
          console.log("POST success recorded at: ",date.getTime());
          if (!dataR){
          console.log("No Data received");
          }else{
              console.log("DataR: ",dataR);
          }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          console.log("Status: " + textStatus+" error:"+ errorThrown);
        },
        timeout:5000
      });

  });
});
Run Code Online (Sandbox Code Playgroud)

使用WebStrom IDE我试图在chrome中运行index.html(它在localhost:63342上运行它)请求成功并按预期传递数据.

然而当我按下按钮时,我会使用Android模拟器模拟应用程序:

POST http://127.0.0.1:3000/tweets net::ERR_CONNECTION_REFUSED -- jquery-3.2.1.min.js:4
Run Code Online (Sandbox Code Playgroud)

基本上.我在本地运行我的node.js(express)服务器,并希望从android模拟器上的cordova应用程序进行ajax调用.我弄错了什么?

Eas*_*ude 7

好吧,经过数小时的研究后,我终于发现localhost或127.0.0.1只是模拟它自己而不是它运行的计算机.要解决此问题,我只需将请求发送到我的计算机(运行node.js服务器)本地IP.在我的情况下,本地IP是192.168.0.2所以我改变了:

http://127.0.0.1:3000/tweets 
Run Code Online (Sandbox Code Playgroud)

至:

http://192.168.0.2:3000/tweets
Run Code Online (Sandbox Code Playgroud)

它似乎现在有效.