dongzhong1929 2014-12-30 13:56
浏览 46


How to fetch the data from the php using json in phonegap, On server xampp, I just write the following code replay.php. I have taken these example from this line and and I want to mention this question from stackover flow, try to make according to it but my android emulator is not resounding, please help me on this topic, I am new in phonegap and android,


header('Content-Type: application/json');
$choice =$_POST["button"];
$cars = array("Honde", "BMW" , "Ferrari");
$bikes = array("Ducaite", "Royal Enfield", "Harley Davidson");
if($choice == "cars") print json_encode($cars);
print json_encode($bikes);

In eclipse I write the following code in index.html

<!DOCTYPE html>
    <script charset="utf&minus;8" type="text/javascript">
    function connect(e)
        var term= {button:e};
        alert("no connection");},
                for(var i in data){
    <center><b>Bikes or Cars</b></center>
    <center><input onclick="connect(this.value)" type="button" value="cars" /></center>
    <center><input onclick="connect(this.value)" type="button" value="bikes" /></center>
    <ul id="result"></ul>
  • 写回答

1条回答 默认 最新

  • duancheng3342 2014-12-30 21:20

    Correct me if I am wrong but you are just asking HOW to perform this process, correct? Your question sounds like that code is from a "tutorial" and not your application? Below is how I have performed this function in one of my own private apps using PhoneGap.

    My AJAX call looks like this, minus custom data manipulations:

        url: "",    // path to remote script
        dataType: "JSON",                                // data set to retrieve JSON
        success: function (data) {                       // on success, do something...
            // grabbing my JSON data and saving it
            // to localStorage for future use.
            localStorage.setItem('myData', JSON.stringify(data));

    My PHP script functions like so:

    // PHP headers (at the top)
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json");
    // connect to your database and perform your query    
    // then build your output...
    while($row = mysqli_fetch_array($result)) {
        $output[] = array (
            "id" => $row['id'],
            "firstname" => $row['firstname'],
            "lastname" => $row['lastname']
    echo json_encode($output);

    This process above will give you a saved localStorage file of the data from the PHP Script (your database). You can then use the localStorage to output your data without having to continually perform AJAX requests.

    For example:

    var myData = JSON.parse(localStorage.getItem('myData'));
    var i;
    for (i = 0; i < myData.length; i = i + 1) {
       console.log(myData[i].firstname);      // this should output the firstnames.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码
  • ¥15 esp32烧录失败,具体情况在图片上