du521521521 2016-10-17 14:39
浏览 34
已采纳

从Ajax发布获得多个结果

Maybe this is a duplicate but I can not understand why mo code does not work. I am trying to get multiple results via Ajax/php.

This is from my php file:

$result11 = 'test1'
$result22 = 'test2';

echo json_encode(array("data1" => $result11, "data2" => $result22));

Ajax call:

$(document.body).on('submit','#sendmessage',function() {

    $.ajax({
        type: "POST",
        url: "/send.php",
        data: {par:par,kid:kid,ha:ha,sform:sform,editors:editors},
        cache: false,
        dataType:'json',
        success: function(datax) {
            alert(datax.data1);
        }

    });
    return false;
});

Problem:

When I submit a form, the page refreshes instead of sending ajax request.

At the same time this works but I can't get multiple results from Php file:

$(document.body).on('submit','#sendmessagex',function() {

    var str = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "/send.php",
        data:str,    
        success: function(data) {
            alert(data);
        }

    });
    return false;
});
  • 写回答

2条回答 默认 最新

  • doupang9080 2016-10-17 14:43
    关注

    Add a preventDefault() call to your script

    $(document.body).on('submit','#sendmessagex',function(event) {
    //----------------------------------------------------^^^^^
        event.preventDefault();
        var str = $(this).serialize();
        $.ajax({
                type: "POST",
                url: "/send.php",
                data:str,    
                success: function(data) {
                    alert(data);
                }
    
        });
        return false;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 编写c++计算器,用类和栈来做,要求如下
  • ¥15 以下代码,运行结果报错
  • ¥15 Vivado仿真数据出错
  • ¥15 银河麒麟不支持vant等前端框架怎么办?
  • ¥15 vue3的子组件在父页面调用不显示不生效问题
  • ¥15 cadence PEX
  • ¥15 phython创建了文件 显示在项目下面,但运行不了,运行时还是运行main文件,并且说main文件不允许并行运行
  • ¥15 FutureWarning:不推荐使用空或全 NA 条目的 DataFrame 串联行为。怎么改呢?