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 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素