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 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令