简体   繁体   English

如何使用phonegap运行外部php脚本?

[英]How to run external php script using phonegap?

I am trying to connect to a remote server and basically run my application off files on that server. 我正在尝试连接到远程服务器,并且基本上从该服务器上的文件运行我的应用程序。 I am able to connect to the server but I don't know how to execute the php files on that server. 我能够连接到服务器,但是我不知道如何在该服务器上执行php文件。 Here's my javascript function on my local directory which connects to the server: 这是我连接到服务器的本地目录上的javascript函数:

    function test2(){
    $.getJSON("http://sampleserver.com/check.php?var=test&callback=?", {
    success:function(data){
        alert("Working"); }, 
    error: function() { 
    alert("Error"); 
}
});

}

This alerts "Working". 这将提醒“工作中”。 Here's my check.php on the remote server: 这是我在远程服务器上的check.php:

<?php
echo "hello";
echo $_GET['jsoncallback'];
?>

Why is hello not being printed to the screen? 为什么没有将您好打印在屏幕上? How would I actually execute this script once I am connected? 连接后如何实际执行此脚本?

您的脚本已经在执行,但是您正在执行AJAX调用,只是用您自己的消息发出警报,因此您对响应不做任何事情,响应中将包含您好文本。

Here is just one example: 这只是一个例子:

var url = '/m.profile_msg/0/send/';
var dataString = 'msg_body='+ msg_body;
$.ajax({
    type: "POST",
    url: url,
    data: dataString,
    success: function(data) {
        try {
            //var data = jQuery.parseJSON(data);
            alert(data);
        } catch(e) {
            alert(data);
        }
    }
});

And on the PHP side: 在PHP方面:

<?php
$xxarray = array('key' => 'value');
echo json_encode($xxarray);
?>

To run remote scripts - try doing your ajax call to a PHP script and have the PHP script shell execute something. 要运行远程脚本,请尝试对PHP脚本进行ajax调用,并让PHP脚本Shell执行某些操作。 But I wouldn't wait for the reply - rather return a status code back to the mobile device saying that the script is running. 但是我不等答复-而是将状态代码返回到移动设备,说脚本正在运行。

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

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