使用的PhoneGap和jQuery不起作用跨域请求 [英] Cross-domain requests using PhoneGap and jQuery doesn't work

查看:157
本文介绍了使用的PhoneGap和jQuery不起作用跨域请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个PhoneGap的应用程序为Android。要获取数据(远程)服务器我做一个REST调用使用jQuery的$。阿贾克斯()函数。有几件事你必须知道的:

I'm creating a PhoneGap app for Android. To get data from the (remote) server I make a REST call using jQuery's $.ajax() function. There are a few things you must know:

  • 呼叫的类型必须是POST
  • 在服务器期待JSON数据(至少用户名和密码)
  • 服务器返回JSON数据

在code:

function makeCall(){
    var url = "http://remote/server/rest/call";

    var jsonData ='{"username":"'+$('#username').val()+'","password":"'+$('#password').val()+'"}';

    $.ajax({
            headers: {"Content-Type":"application/json; charset=UTF-8"},
            type: "POST",
            url: url,
            data: jsonData,
            dataType: "json",
            success: succesFunction,
            error: errorFunction
    });
}

不过,这也不行。当我使用Firebug看到服务器的回应,并没有什么。随着TcpTrace我可以看到请求的报头。代替一个预期POST方法的,有一个OPTIONS方法,用一些奇怪头添加

But, this doesn't work. When I use Firebug to see the servers response, there is nothing. With TcpTrace I can see the headers of the request. Instead of an expected POST method, there is an OPTIONS method, with some strange headers added.

OPTIONS /remote/server/rest/call HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: nl,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Origin: null
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Pragma: no-cache
Cache-Control: no-cache

我知道它是与做跨域请求,但我不知道如何解决这个问题。我试了几件事情来解决它,但没有结果:

I know it has something to do with doing cross-domain requests, but I don't know how to solve the problem. I tried a few things to fix it, but with no result:

这个问题也有一些做同样的原产地政策,但这并不适用于该文件://协议的PhoneGap是使用加载本地HTML文件

The problem has also something to do with same origin policy, but this does not apply to the file:// protocol PhoneGap is using to load a local html file.

在我的Andr​​oidManifest.xml文件,选项

In my AndroidManifest.xml file, the option

<uses-permission android:name="android.permission.INTERNET" />

设置。

我想解决这个问题2现在天,但没有结果到现在。这甚至可能做什么?你有什么建议给我,所以我可以继续前进?

I'm trying to fix this for 2 days now, but no result till now. Is this even possible to do? Do you have any tips for me so I can move on?

在此先感谢!

推荐答案

我由我自己解决了这个问题。问题是地处网址,在那里我有添加域。我改变了

I solved the problem by myself. The problem is located in the url, where I have to add a domain. I changed

VAR URL =HTTP://远程/服务器/ REST /呼叫;

VAR URL =htt​​p://remote.mydomain.com/server/rest/call;

和它的作品!

我认为第一个URL应该工作,因为它的工作原理与确切相同的URL,并设置一个iPhone应用程序。它也是东西做一个双重防火墙(Windows和ESET防火墙),我关闭了Windows防火墙。

I assumed the first url should work because it works on an iphone app with exact the same url and settings. It has also something to do with a double firewall(Windows and ESET firewall) where I shut down the Windows firewall.

无论如何,感谢您的回答!

Anyway, thanks for your answers!

这篇关于使用的PhoneGap和jQuery不起作用跨域请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆