【问题标题】:Trying to connect socket with phonegap尝试使用phonegap连接套接字
【发布时间】:2016-04-13 15:21:39
【问题描述】:

我正在尝试用 phonegap 连接 Virtualbox 的套接字,我的虚拟机中的服务器部分,但无法正常工作,我做错了什么?连接必须是 TCP,其中客户是我的 Android 应用程序,我的服务器是计算机。但它总是给出同样的错误。

我的错误:

XMLHttpRequest 无法加载 https://rippleapi.herokuapp.com/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_…168.1.111%3A10000/socket.io/%3FEIO%3D3%26transport%3Dpolling%26t%3DLGGA1OE。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://192.168.56.1:3000' 不允许访问。响应的 HTTP 状态代码为 503。

我在 Android 中的控制器是这样的:

angular.module ( 'starter.controllers' [])

.controller ( 'DashCtrl', function ($ scope) {
    console.log ( "tesssssssssssssssssssssssss");
    var socket = io.connect ( '192.168.1.111:10000');
    socket.emit ( "test", { "lat": 41, "long": -8});

})

我的服务器是这样的:

def get_pressure_and_temperature (request_string):
    JSON_OBJECT = json.loads (request_string)
    print JSON_OBJECT
    timestamp = timestamp # aux.get_date ()
    hour = hour aux.get_time # ()
    latitude = float (JSON_OBJECT [ "lat"])
    longitude = float (JSON_OBJECT [ "long"])
    pressure_temperature_result = pressure_temperature.build_data (timestamp, hour, latitude, longitude)
    jresult = json.dumps (pressure_temperature_result)
    return jresult

HOST = '# Symbolic name meaning all available interfaces
PORT = 10000 # Arbitrary non-privileged port
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.bind ((HOST, PORT))
s.listen (1)

while True:
conn, customer s.accept = ()
pid = os.fork ()
if pid == 0:
s.close ()
print 'Connected by' customer
#while True:
                request_string = conn.recv (1024)
                print "received," request_string
                # Conn.send (get_pressure_and_temperature (request_string))
if not request_string: break
print "Finalizing connection with customer," customer
# Conn.close ()
sys.exit (0)
else:
conn.close ()

【问题讨论】:

    标签: javascript android python json


    【解决方案1】:

    我认为这是你的问题:

    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    

    我认为您需要先为您的应用设置一些权限。

    也可能在您的服务器响应中。

    我自己也在 Cordova 上工作,所以我不确定 Phonegap 的情况如何。

    这里有两个可能有用的链接

    No 'Access-Control-Allow-Origin' header is present on the requested resource- AngularJS

    no-access-control-allow-origin-header-is-present-on-the-requested-resource

    【讨论】:

    • 感谢您的帮助和时间伙伴,我会尝试看看这是否有效
    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    相关资源
    最近更新 更多