Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

javascript - die(json_encode)) redirects to a page displaying json data

When form submission finishes, the page DISPLAYS the raw json data instead of logging it to the console. The php and html code are both on the same page so I wasn't expecting the page to change at all.

POST

jQuery(function($) {
  $("#idSearch").submit(function(event) {
    $.ajax({
        url: "/index.php",
        type: "POST",
        data: $(this).serialize(),
        dataType: "json",
        sucess: function(data) {
            console.log(data);
        }
    })
  })
});

php form handling

<?php 
if (isset($_POST['orderId'])){
    header('Content-Type: application/json');
    require 'private/database.php';

    $sql = "SELECT * FROM form";
    $result = mysqli_query($conn, $sql);

    $data = array();
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $data[] = $row;
        }
    }
    die(json_encode($data));
}
?><!DOCTYPE html>

I've implemented something similar on another webpage but it works as I intended.

POST

function loadInfo() {
    jQuery(function($) {
        $.ajax({
            method: "POST",
            url: "/admin.php",
            data: {loadInfo: 1},
            dataType: "json",
            success: function(data) {
                console.log(data);
                for (var i = 0; i < data.length; i++) {
                    setMarker(data, i, "red");
                    printInfo(data, i);
                }
            }
        })
    });
}

php form handling

<?php
if(isset($_POST['loadInfo'])){
    header('Content-Type: application/json');
    require 'private/database.php';

    $sql = "SELECT * FROM form";
    $result = mysqli_query($conn, $sql);

    $data = array();
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $data[] = $row;
        }
    }
    die(json_encode($data));
}
?><!DOCTYPE html>

Why do these two pages behave differently?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should stop the default form event. The browser continue the action and submit the form, so the page is reloaded using POST.

You could prevent using Event.preventDefault()

jQuery(function($) {
  $("#idSearch").submit(function(event) {
    event.preventDefault(); // << ADD
    $.ajax({
        url: "/index.php",
        type: "POST",
        data: $(this).serialize(),
        dataType: "json",
        success: function(data) { // << Here 'success' not 'sucess'.
            console.log(data);
        }
    })
  })
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...