0

我是移动 Web 应用程序开发的新手……现在我正在努力使用 mosync sdk 连接 myphpadmin……

$(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);
        }
    });
});

问题:我将gene.php文件放在localhost中它运行良好,但如果我加载文件是服务器(http://xxxxxx.in/NewsRecord.php)它不起作用。我不知道如何解决这个问题?帮帮我谢谢提前

4

1 回答 1

0

实际上我试图从另一台服务器访问文件,没有许可是不可能的,浏览器不允许这样做。然后我在回调中使用 JSONP 解决了这个问题。

其他服务器文件: http: //xxxxxx.in//NewsRecord.php ?callback= ?

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

每当我运行此链接时,它都会像这样打印 JSONP数据({“result”:“value”}),我将 dataType 更改为 JSONP,现在它工作正常

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

}
});
于 2014-03-18T10:09:32.657 回答