Running a php script on a phonegap application

时间:2015-04-23 05:16:32

标签: javascript php android ajax cordova

I am building an app that pulls data from a database. I am using ajax to run the php script which is located on my external server and attempting to return the data is json format. Every time I attempt to run the function that calls the script I get an error stating that the URL can not be found. I have added my server domain to the whitelist in my config file. I'm not sure what else to do and any help would be appreciated.

Here is the code.

index.html javascript to run the php script:

$(document).on("pagebeforeshow", "#articlelist", function(){
        $.ajax({
            url: "http://domain/App/script/getArticleList.php",
            type: "POST",
            dataType: "json",
            data:{catid: catid},
            success: function(data){
                alert("success");
                $("#article-list").empty();
                var li="";
                $.each(data, function(i,item){
                    li += '<li><a href="" data-class="artSelection" id="'+data[i].id+'">'+data[i].title+'</a></li>';
                });

                $("#article-list").append(li).promise();
                $("#article-list").listview('refresh');
            },
            error:function(x,e){
                if(x.status==0){
                    alert('You are offline!!\n Please Check Your Network.');
                }else if(x.status==404){
                    alert('Requested URL not found.');
                }else if(x.status==500){
                    alert('Internel Server Error.');
                }else if(e=='parsererror'){
                    alert('Error.\nParsing JSON Request failed.');
                }else if(e=='timeout'){
                    alert('Request Time out.');
                }else {
                    alert('Unknow Error.\n'+x.responseText);
                }
            }
        });

getArticleList.php:

<?php
    if (isset($_POST['catid'])){
        $server = "localhost";
        $username = "username";
        $password = "password";
        $database = "database";

        $con = mysqli_connect ($server, $username, $password, $database) or die ('Could not connect: ' . mysqli_connect_error());
        mysqli_set_charset($con,"utf8");

        $list = array();
        switch($catid){
            case "catid=21":
                $qry = mysqli_query($con, "SELECT title, id FROM u9wkx_content WHERE ".$catid." AND publish_down >= NOW() ORDER BY `ordering` ASC");
                break;
            case "catid=90":
                $qry = mysqli_query($con, "SELECT title, id FROM u9wkx_content WHERE ".$catid." AND publish_down >= NOW() ORDER BY `title` ASC");
                break;
            case "catid=19":
                $qry = mysqli_query($con, "SELECT title, id FROM u9wkx_content WHERE ".$catid." AND publish_down >= NOW() ORDER BY `ordering` ASC");
                break;
            case "catid=107":
                $qry = mysqli_query($con, "SELECT title, id FROM u9wkx_content WHERE ".$catid." AND publish_down >= NOW() ORDER BY `ordering` ASC");
                break;
            case "catid=42":
                $qry = mysqli_query($con, "SELECT title, id FROM u9wkx_content WHERE ".$catid." AND publish_down >= NOW() ORDER BY `ordering` ASC");
                break;
            default:
                $qry = mysqli_query($con, "SELECT title, id FROM u9wkx_content WHERE ".$catid." AND publish_down >= NOW()");
        }

        while($row = mysqli_fetch_assoc($qry)){
        $list[] = $row;
        }
    }


    header('Content-type: application/json');
    $list_data = json_encode($list);

    echo $list_data;
?>

0 个答案:

没有答案