Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用JSONP在cordova应用程序上显示PHP生成的页面?_Javascript_Php_Ajax_Cordova_Jsonp - Fatal编程技术网

Javascript 如何使用JSONP在cordova应用程序上显示PHP生成的页面?

Javascript 如何使用JSONP在cordova应用程序上显示PHP生成的页面?,javascript,php,ajax,cordova,jsonp,Javascript,Php,Ajax,Cordova,Jsonp,我试图做的是在mySQL中存储路径,然后在服务器上存储图像。然后使用下面的PHP代码将它们加载到屏幕上,并使用按钮浏览它们。这在浏览器上很好。然而,这些操作实际上将在不接受PHP的cordova应用程序上完成,并且我不能使用HTTP请求来执行我的功能。有人能帮我转换代码和显示图像吗。我认为最好的选择是使用JSONP或AJAX和JSONP的组合 下面是我试图使用HTTP请求显示的PHP端的代码 <!DOCTYPE html> <html> <?php include

我试图做的是在mySQL中存储路径,然后在服务器上存储图像。然后使用下面的PHP代码将它们加载到屏幕上,并使用按钮浏览它们。这在浏览器上很好。然而,这些操作实际上将在不接受PHP的cordova应用程序上完成,并且我不能使用HTTP请求来执行我的功能。有人能帮我转换代码和显示图像吗。我认为最好的选择是使用JSONP或AJAX和JSONP的组合

下面是我试图使用HTTP请求显示的PHP端的代码

<!DOCTYPE html>

<html>
<?php
include("mysqlconnect.php");

$select_query = "SELECT `ImagesPath` FROM `offerstbl` ORDER by `ImagesId` DESC";
$sql = mysql_query($select_query) or die(mysql_error());   
$data = array();
while($row = mysql_fetch_array($sql,MYSQL_BOTH)){
$data[] = $row['ImagesPath'];
}

$images = json_encode($data);

?>

<script> 

var images = <?php echo $images; ?>
alert(images[1]);


var index = 0;

function buildImage() {
var img = document.createElement('img')
img.src = images[index];
document.getElementById('content').appendChild(img);
}

function changeImage(){
var img = document.getElementById('content').getElementsByTagName('img')[0]
index++;
index = index % images.length; // This is for if this is the last image then goto first image
img.src = images[index];
}
</script>




<body onload="buildImage();">
<div class="contents" id="content"></div>
<button onclick="changeImage()">NextImage</button>

</body>
</html>
这是我用来显示PHP代码的cordova中的HTML代码。但是,我的JavaScript函数都不起作用。我认为它需要JSONP的元素

<!DOCTYPE html>
<html>
<head>

<script>
function showUser(str) {
    if (str=="") {
        document.getElementById("phpFile").innerHTML="";
        return;
    }
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("content").innerHTML=xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET","http:server/content.php?");
    xmlhttp.send();

}


</head>

<body onload="showUser()">

<div class="contents" id="phpFile">


</body>
</html>

因为您运行的是应用程序而不是实际的浏览器,所以不适用相同的域策略

我必须在config.xml中指定我想通过应用程序访问的任何URL

<widget xmlns = "http://www.w3.org/ns/widgets" id = "io.something.something" version = "1.6">
     <access origin="http://127.0.0.1*"/> <!-- allow local pages -->
     <access origin="https://mydomain.com" subdomains="true" />
</widget>

在上面的代码中,运行php文件的html元素没有问题。所以这不是权限问题。谢谢你。应用程序上正在运行的功能的版本号。我将你的代码添加到我的配置文件中只是以防万一,但仍然没有乐趣。