dongxieting9623 2015-11-02 16:02
浏览 80
已采纳

如何在不使用JSON的情况下进行AJAX调用? [关闭]

I am trying to improve my jQuery and AJAX knowledge and I wanted to create a simple call to my database. However, I am having some issues with it and I can't really understand why. Here is the code:

function provaajax() {
    $.ajax({
        url: 'visualizza.php',           
        data: "",                      
        dataType: 'text',               
        complete: function(data) {
            var nome = data[0];
            var cognome = data[1];
            $('.provajax').html("<b>nome: </b>" + nome + "<b> cognome: </b>" + cognome);
        }
    });
}

And here is my PHP file:

<?
    Connection();
    $result = mysql_query("SELECT * FROM prova WHERE ID = 117");
    $array = mysql_result($result, 0, "Nome");
    $array = mysql_result($result, 0, "Cognome");
    echo $array;
?>

And a little code from my HTML:

<div>
    <button onclick="provaajax()">Prova Ajax</button>
    <p class="provajax"> Hello world </p>
</div>

But this code isn't working. I don't know how to use JSON, so it would be better not to use it. Any ideas?

  • 写回答

1条回答 默认 最新

  • dsztc99732 2015-11-02 16:08
    关注

    To retrieve the nome and cognome values from your PHP it would probably be easiest to return JSON. To do that you can create an array with those values and encode it with json_encode:

    <?
        Connection();
        $result = mysql_query("SELECT * FROM prova WHERE ID = 117");
        $arr = array('nome' => mysql_result($result, 0, "Nome"), 'cognome' => mysql_result($result, 0, "Cognome"));
        echo json_encode($arr);
    ?>
    

    Then you can retrieve these values from the deserialised object provided to the success function by jQuery:

    complete: function(data) {
        var nome = data.nome;
        var cognome = data.cognome;
        $('.provajax').html("<b>nome: </b>" + nome + "<b> cognome: </b>" + cognome);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,