如何在phonegap中使用xmlHttpRequest来访问php?

问题描述 投票:0回答:1

我正在尝试首次构建phonegap应用。我正在测试一个基本的xmlhttprequest。如果我能使这个基本请求正常工作,我将使用php从mysql提取数据,但我需要首先使php工作。

我在stackoverflow上尝试了很多其他建议。我已经使用白名单插件来允许外部连接。我已经将header('Access-Control-Allow-Origin: *');添加到了我的PHP中我添加了

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

到HTML什么都行不通...

这是我的html

<html>

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


    <head>
        <style>
            html, body { padding:0;margin:0;background:#231f20;color:#fff }
        </style>
    </head>
    <body>
        <div style="position:relative;width:100%;height:100%">
            <div style="position:absolute;width:90%;height:90%;background:#484042;padding-left: 1%">
                <br>
                <div style="background-color: orange;width:30vw;height: 30vw" onclick="test()"></div>
                <div id = "div_" style="color:green;background-color: pink;width: 30vw;height: 30vw "></div>
            </div>
        </div>
    </body>
</html>

这是我的JavaScript

function test(){

        var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                  document.getElementById('div_').innerHTML = this.responseText;
                }

            };
            xmlhttp.open("GET", "http://appserver/app/load.php?", true);
            xmlhttp.send();
    }

这是我要调用的php:

<?php
header('Access-Control-Allow-Origin: *');
echo "heloooooooooo!!!!";
?>

这是从我的config.xml文件中

<plugin name="cordova-plugin-whitelist"      source="npm" spec="1.1.0" />
    <allow-navigation href="*" />
    <allow-intent href="*" />
    <access origin="*" />

    <platform name="android" />
    <preference name="phonegap-version" value="3.1.0" />
javascript php html xmlhttprequest phonegap
1个回答
0
投票

您有没有解决这个问题?我自己也面临着同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.