Search code examples
phpjqueryajaxmobilemosync

can't able to access php file from serverside


im newbie to mobile web app development ... now im struggling to connect myphpadmin using mosync sdk...

$(document).ready(function () {
    $.ajax({
        url: "http://192.168.2.136:80/ajaxjson/gene.php",
        dataType: 'text',
        success: function (output) {
            var i = $.parseJSON(output);
            //  $('#one').append('<p style="font-weight:bold">NEWS LETTER</P>');
            for (var j = 0; j < i.length; j++) {
                var title_temp = i[j].Title;
                var title_resized = title_temp.substring(0, 30);
                title_resized = title_resized + "...";

                $('#one').append('<div><p><span style="font-weight:bold" >TITLE</span> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp: &nbsp; &nbsp;<small><a href=' + i[j].links + '>' + title_resized + '</a></small><br><span style="font-weight:bold" >SOURCE</span> &nbsp; &nbsp;&nbsp;&nbsp; :  &nbsp; &nbsp;' + i[j].Source +
                    '<br><span style="font-weight:bold" >CATEGORY</span> &nbsp;: &nbsp; &nbsp;' + i[j].Category + '<hr><p></div>');
                //$('#one').append('<p><div style="background-color:#ccc"><span style="font-weight:bold" >SOURCE</span> &nbsp; &nbsp;&nbsp;&nbsp; :  &nbsp; &nbsp;'+i[j].Source+'<p>');
                //$('#one').append('<p><div style="background-color:#ccc" onclick="get"><span style="font-weight:bold" >CATEGORY</span> &nbsp;: &nbsp; &nbsp;'+i[j].Category+'<hr><p></div>');
            }
        },
        erro: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.statusText);
            alert(thrownError);
        }
    });
});

Problem: I put gene.php file in localhost it is working good but if i load the file is server (http://xxxxxx.in/NewsRecord.php) it is not working.I dono how to fix this?help me thanks in advance


Solution

  • Actually I tried to access file from another server and It is not possible without the permission and browser wont allow to do that. Then I solved the problem using JSONP in callback.

    Other Server file : http://xxxxxx.in//NewsRecord.php?callback=?

    echo $_GET['callback'].'('.json_encode($output).')';
    

    whenever i run this link it print JSONP data ({"result":"value"}) like this and i changed dataType to JSONP and its working fine now

    $(document).ready(function () {
        $.ajax({
            url: "http://xxxxxx.in//NewsRecord.php?callback=?",
            dataType: 'jsonp',
            success: function (output) {
                  //working now
                        alert(output.result);
    
    }
    });