PhoneGap的跨域Ajax和PhoneGap的开发应用程序和CLI应用程序、PhoneGap、Ajax、CLI

2023-09-10 18:06:32 作者:血染丶残花

在与PhoneGap的开发应用和CLI GET通话和POST 没有数据做AJAX的跨域调用的成功,POST数据呼叫不工作。如果试图地方,一切工作正常。

When doing ajax cross domain calls with PhoneGap developer app and CLI only GET calls and POST with no data success, POST call with data is not working. If trying local, everything is working fine.

我没有config.xml中设置获得原产地=*。 还设立 $ support.cors = TRUE; 脚本

I did set in config.xml access origin="*". Also did set $.support.cors = true; in script.

下面的例子中,我做在Hello World应用程序从PhoneGap的。

Here is example I am doing on Hello world app from PhoneGap.

    <script type="text/javascript">
    $(document).bind("ready", function() {
        $.support.cors = true;
        test();
    });
    </script>

    <script>
    function test(){
        $.ajax({
            url:'http://www.url.com',
            type:'POST',
            headers:{
                "Content-Type":'application/x-www-form-urlencoded'
            },
            data:'x=1',
            cache:false,
            success: function (data) {
                alert(data);
            },
            error:function(request, textStatus, errorThrown){
                alert(errorThrown);
            }
        });
        return false;
    }
    </script>

有趣的是,当看到控制台,通话后,呼叫不进行显示的时候,但经过一段时间。响应时间是超长的,它需要更多的则15秒的反应,而当最后得到的响应存在对阿贾克斯没有错误。然而,在控制台中,我得到的错误,代理服务器错误:URL:输入的URLECONNRESET

我试图做同样的要求槽REST控制台和有同样的效果。

I have tried doing same request trough REST console and there is same effect.

代理的URL是

http://localhost:3000/proxy/

我还没有做出任何更改,代理,我想如果可能的话禁用它,但如果没有,只是能跨域。

I haven't made any changes to proxy, and I would like to disable it if possible, but if not, just enable cross domain.

网站到我做跨域调用启用跨域调用,而我在纹波的应用程序测试,一切工作正常。另外,如果我建立与PhoneGap的构建应用程序,一切都还在研究smarthphone(的iOS / iPhone)。

Website to which I am doing cross domain call have enabled cross domain calls, and I have app tested in Ripple and everything is working fine. Also if I build app with PhoneGap build, everything is also working on smarthphone (iOS/iPhone).

请注意,如果我测试与脉动,只有远程代理工作和残疾人的代理程序,本地没有工作。这或许表明当地的问题,但我是新来的代理和PhoneGap的。

Note, If I am testing app with Ripple, only remote proxy works and disabled proxy, local is not working. This maybe indicates local problems but I am new to proxy and PhoneGap.

推荐答案

我已经找到解决办法,这不是与PhoneGap的,CLI或Node.js的,但我可以用的PhoneGap开发的应用程序进行测试,这是最主要的,所以我不'T每次都重新构建应用程序。

I have found workaround, it's not with phonegap-cli or node.js, but I can use phonegap developer app for testing which is main thing, so I don't have to rebuild app every time.

如果您在计算机上安装了Apache,你可以告诉代替的PhoneGap-CLI 的PhoneGap开发的应用程序,听取标准端口(80):3000 ,它会成功加载应用程序。通常情况下使用Apache(XAMPP或wampp)工作时,您使用的是本地主机或你可能创建的虚拟主机将无法正常工作,你必须要找到你的本地网络的IP,通常是 192.168.1.X ,其中 X 是您的计算机的数量。这是同一个IP的PhoneGap-CLI节目听,开始时服务

If you have installed Apache on your computer, you can tell phonegap developer app to listen standard port (80) instead of phonegap-cli :3000 and it will successfully load app. Normally when working with Apache (xampp or wampp), you are using localhost or you maybe create vhost which will not work, you have to find out your local network IP, which is usually 192.168.1.X, where X is number of your computer. That is the same IP phonegap-cli shows to listen, when starting serve.

您可以通过将CMD-> ipconfig并有IPv4地址找到它的窗口。通过输入测试它的http:// IP / 在浏览器地址栏,它应该引领你到根本地主机,这是一样的的文件夹HTTP://本地主机/ ,如果已安装的Apache。就我而言,我使用的XAMPP(wampp),所以它的htdocs在XAMPP文件夹中。

You can find it on windows by going CMD->ipconfig and there IPv4 Address. Test it by entering http://IP/ in browser URL bar, and it should lead you to root folder of local host, which is same as http://localhost/, if you have installed Apache. In my case, I am using XAMPP (wampp) so it is htdocs in xampp folder.

现在,你也应该把你的应用程序的项目文件夹中,以便您可以访问它的简单。如果你的项目是存在的,你应该能够将 HTTP访问它:// IP /项目文件夹/ 。所以,你键入路径的PhoneGap开发的应用程序,它应该工作!

Now, you should place your app project also in that folder so you can access it simple. If your project is there you should be able to access it by going to http://IP/project-folder/. So you type that path to phonegap developer app and it should work!

请注意,如果您使用PhoneGap的项目结构,你可能会添加 WWW 来路的最后,我之前提到的,所以它看起来像的http:// IP /项目文件夹/ WWW / 。我不使用PhoneGap的标准文件夹结构,它是为我工作。

Note, if you are using phonegap project structure, you will probably have to add www to end of path I mentioned before, so it will look like http://IP/project-folder/www/. I am not using phonegap standard folder structure and it is working for me.

另外,我没有使用太多的本地函数,所以我不能告诉你它的工作100%。我的主要目标是让跨域调用的工作,并且能够无需重建应用程序的任何变化进行测试。

Also, I am not using much native functions, so I can't tell if it's working 100%. My main goal was to have cross domain calls working, and to be able to test it without rebuilding app for any change.

外,像3/4手指轻点快捷方式不会因为你不使用Node.js的在这种情况下,所以没有反馈,从计算机的PhoneGap开发应用工作。

Extra, shortcuts like 3/4 finger tap will not work as you are not using node.js in this case so there is no feedback to phonegap developer app from computer.

建议,增加地方键来重新加载应用程序,当你做出改变,这样你就不必在每次你做更改时退出应用程序。

Advice, add somewhere button to reload app when you make changes so that you don't have to quit app every time you do change.