简体   繁体   English

PhoneGap Build:AJAX无法正常工作

[英]PhoneGap Build: AJAX not working

I've been trying to make a little test app that makes an AJAX call. 我一直在尝试制作一个可以进行AJAX调用的小测试应用程序。 When I run it using the PhoneGap Desktop server and Android developer app. 当我使用PhoneGap桌面服务器和Android开发者应用程序运行它时。 everything works fine. 一切正常。 But when I use PhoneGap Build to compile the app, install the .apk on my phone. 但是,当我使用PhoneGap Build编译应用程序时,请在手机上安装.apk。 the AJAX call fails. AJAX调用失败。

I've read all over to try to find a solution to my problem. 我已经阅读了所有尝试找到问题的解决方案。 seems there are a lot of people having this issue as well, but so far I haven't been able to fix it. 似乎有很多人也有这个问题,但到目前为止我还没能解决它。 The most common thing I see people saying is that it's a whitelist issue. 我看到人们说的最常见的事情是它是白名单问题。 But my config.xml file contains... 但是我的config.xml文件包含......

<plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1"/>

as well as... 以及...

<access origin="*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>

And my CSP looks like this... 我的CSP看起来像这样......

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

Which according to Cordova documentation should allow AJAX. 根据Cordova文档应该允许AJAX。

My app is very simple. 我的应用很简单。 It has a button which calls my AJAX function and passes it some info to send to the server, and a callback function... 它有一个按钮,它调用我的AJAX函数并传递一些信息发送到服务器,以及一个回调函数...

<button onclick="serv('init',ajaxer)">BUTTON</button>

Here is my js file: 这是我的js文件:

function ajaxer(resp){
    var e = document.getElementById("test");
    e.innerHTML="responded...";

    if(resp.go){
        e.innerHTML="IT WORKED!";
    }else{
        e.innerHTML="foo";
    }
}

function serv(request,returnFunc,parameters) { //SEND AND RECEIVE ALL SERVER INFO
    document.getElementById("test").innerHTML="sending info...";    

    var query="";
    var serverObject;
    if(window.XMLHttpRequest){
        serverObject = new XMLHttpRequest();
    }else{
        serverObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    serverObject.goto = returnFunc;

    serverObject.onreadystatechange = function() {
        if(serverObject.readyState==4&&serverObject.status==200){
            var serverResponse = JSON.parse(serverObject.responseText);
            serverObject.goto(serverResponse);
        }
    };

    if(parameters){
        query="&"+parameters;
    }

    serverObject.open("POST", "http://tylermackenzie.duckdns.org/phoneGap%20Test/server.php", true);
    serverObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    serverObject.send('qType='+request+query);
}

When I press the button, it displays the text "sending info..." but never switches it to "responded..." or "IT WORKED!" 当我按下按钮时,它会显示文本“发送信息...”,但从不将其切换为“响应...”或“它工作!” or "foo". 或“foo”。 which means that ajaxer() was never called. 这意味着从未调用过ajaxer()。

I can't understand why it's not working. 我不明白为什么它不起作用。 Like I mentioned earlier. 就像我之前提到的那样 it works great when I use the phoneGap developer app. 当我使用phoneGap开发者应用程序时它很有用。 And I've got my phone wifi turned off while I test it. 我在测试时关闭了手机wifi。 So I know that the phone is on a different network then the server because the phone has to use the mobile network. 所以我知道手机与服务器位于不同的网络,因为手机必须使用移动网络。 If this is just a whitelist issue, shouldn't it have the same problem when I test it with the developer app? 如果这只是白名单问题,那么当我使用开发者应用程序测试它时,它是否应该有同样的问题?

Also, I know it's not a problem with my server because it responds fine when I test on developer app, and when I navigate there manually. 另外,我知道这对我的服务器来说不是问题,因为当我在开发者应用程序上测试时,以及当我手动导航时,它会做出很好的响应。

Hoping someone can help me out. 希望有人可以帮助我。

ADDITION 加成

could it have something to do with my file structure that I'm uploading to PhoneGap Build? 它可能与我上传到PhoneGap Build的文件结构有关吗? I just used the "hello world" basic app that the PhoneGap Desktop App creates. 我刚刚使用了PhoneGap桌面应用程序创建的“hello world”基本应用程序。 And edited the html and js to create a little test app. 并编辑了html和js来创建一个小测试应用程序。

It's a little confusing to me because there is an index.html under AppName/www as well as AppName/platforms/browser and under AppName/platforms/browser/www Why are all the files tripled? 这对我来说有点混乱,因为在AppName / www以及AppName / platforms / browser下有一个index.html,在AppName / platforms / browser / www下面为什么所有文件都是三倍的? I tried making all three copies be the same thing. 我试着让所有三个副本都是一样的。 doesn't seem to make any difference. 似乎没有任何区别。

I read somewhere that I only need to upload the AppName/www folder to PhoneGap Build, and it seems to work just as good as uploading everything else. 我在某处读到了我只需要将AppName / www文件夹上传到PhoneGap Build,它似乎和上传其他内容一样好。

Have you tried adding a connect-src section to your Content Security Policy? 您是否尝试过将connect-src部分添加到内容安全策略中? For example: 例如:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; connect-src *">

This controls where Ajax requests can be made to, see documentation at content-security-policy.com 这可以控制Ajax请求的位置,请参阅content-security-policy.com上的文档

I recommend you to use jQuery to make ajax requests it is more manageable and maintainable also it is more easy to make ajax request via jQuery 我建议你使用jQuery来制作ajax请求它更易于管理和维护也更容易通过jQuery发出ajax请求

also you can debug phonegap application and see how ajax request goes and return to server and from server by using weinre remote debugger you can follow steps presented in the following tutorial t make this Weinre Remote Debugging Tutorial 你也可以通过使用weinre远程调试器调试phonegap应用程序并查看ajax请求如何进入并返回服务器和服务器,你可以按照以下教程中介绍的步骤来制作这个Weinre远程调试教程

I hope my answer would be useful 我希望我的答案会有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM