I am having trouble making an PHP API to get data from MYSQL and parse JSON. I have a demo app (hiApp) and comes with some JSON files inside a folder.
The demo JSON file is like this:
{ "err_code": 0, "err_msg": "success", "data": [{"nickname":"Joao","location":"I.13"},{"nickname":"Victor","location":"2811"}]}
This what my contacts.php is returning:
[{"nickname":"Joao","location":"I.13"},{"nickname":"Victor","location":"2811"}]
My contacts.php api looks like this: …
…
    $result = mysql_query("select * from sellers", $db);  
    $json_response = array();
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $row_array['nickname'] = $row['first_name'];
        $row_array['location'] = $row['territory'];
        array_push($json_response,$row_array);
    }
    echo json_encode($json_response);
?>
The js file to parse JSON, looks like this:
define(['utils/appFunc',
        'i18n!nls/lang',
        'components/networkStatus'],function(appFunc,i18n,networkStatus) {
    //var apiServerHost = window.location.href;
    var xhr = {
        search: function(code, array){
            for (var i=0;i< array.length; i++){
                if (array[i].code === code) {
                    return array[i];
                }
            }
            return false;
        },
        getRequestURL: function(options){
            //var host = apiServerHost || window.location.host;
            //var port = options.port || window.location.port;
            var query = options.query || {};
            var func = options.func || '';
            var apiServer = 'api/' + func + '.php' +
                (appFunc.isEmpty(query) ? '' : '?');
            var name;
            for (name in query) {
                apiServer += name + '=' + query[name] + '&';
            }
            return apiServer.replace(/&$/gi, '');
        },
        simpleCall: function(options,callback){
            options = options || {};
            options.data = options.data ? options.data : '';
            //If you access your server api ,please user `post` method.
            //options.method = options.method || 'GET';
            options.method = options.method || 'POST';
            if(appFunc.isPhonegap()){
                //Check network connection
                var network = networkStatus.checkConnection();
                if(network === 'NoNetwork'){
                    hiApp.alert(i18n.error.no_network,function(){
                        hiApp.hideIndicator();
                        hiApp.hidePreloader();
                    });
                    return false;
                }
            }
            $$.ajax({
                url: xhr.getRequestURL(options) ,
                method: options.method,
                data: options.data,
                success:function(data){
                    data = data ? JSON.parse(data) : '';
                    var codes = [
                        {code:10000, message:'Your session is invalid, please login again',path:'/'},
                        {code:10001, message:'Unknown error,please login again',path:'tpl/login.html'},
                        {code:20001, message:'User name or password does not match',path:'/'}
                    ];
                    var codeLevel = xhr.search(data.err_code,codes);
                    if(!codeLevel){
                        (typeof(callback) === 'function') ? callback(data) : '';
                    }else{
                        hiApp.alert(codeLevel.message,function(){
                            if(codeLevel.path !== '/')
                                mainView.loadPage(codeLevel.path);
                            hiApp.hideIndicator();
                            hiApp.hidePreloader();
                        });
                    }
                }
            });
        }
    };
    return xhr;
});
I know the error is in the way contacts.php is displaying the JSON results or I need to change something in the js file.
Thanks for the help.
 
    