weixin_33734785 2012-10-26 13:17 采纳率: 0%
浏览 20

phonegap api调用不起作用

I have a rails api running on localhost:3000. On my phonegap app i'm trying to login via api , but it give an error("error") and don't reach to the api. could be some problem with cross domain request, but is said that in the mobile there is no such thing. internet is working .

my ajax:

$.ajax({
        url: 'localhost:3000/api/v1/login',
        type: "POST",
        dataType: "json",
        data : {username:"demo",password:"demo"},
        success: function(data) {
           navigator.notification.alert("ok");
        },
        error: function(xhr, textStatus, errorThrown) {
              console.log("Error status " + status);
        }
});    

EDIT 2 - solved

You have to to use local ip eg. 192.168.2.2 and add the host in the Cordova.plist in the ExternalHosts key.

  • 写回答

1条回答 默认 最新

  • weixin_33724570 2012-10-26 13:25
    关注

    It is because you are using "localhost" when you run this code on a device/emulator then "localhost" refers to the device/emulator itself and not the web server that contains your rails API. You'll need to switch "localhost" to the IP address or hostname of your server.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器