简体   繁体   中英

Ajax doesn't get to success function. Always error function. It fails to post data to another file

Please be patient. This is my first post as far as I remember.

This is a part of my calendar.js script. I'm trying to POST data that I fetch from modal window in index.php to sql.php .

function saveModal() { /*JQuery*/
    var current_date_range = $(".modal-select#daterange").val();
    var current_room_number = $("#mod-current-room-number").val();
    var current_room_state = $("#mod-current-room-state").val();
    var myData = {"post_date_range": current_date_range, "post_room_number": current_room_number, "post_room_state": current_room_state};
    var myJSON = JSON.stringify(myData);

    $.ajax({
        type: "POST",
        url: "sql.php",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        data: myJSON,
        beforeSend: function() {
            $("#ajax").html("<div class='loading'><img src='/images/loader.gif' alt='Loading...' /></div>");
        },
        success: function(result){
            $("#ajax").empty();
            $("#ajax").html(result);
            $("#ajax").fadeIn("slow");
            window.location.reload(true);
        },
        error: function(){
            alert(myJSON);
            $("#ajax").html("<p class='error'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Oops!</strong> Try that again in a few moments.</p>");
        }
    })    
}

I get the data just fine (as you can see I have checked in the error: function() with alert(myJSON); ). It looks like this: {"post_date_range":"12/19/2018 - 12/28/2018","post_room_number":"118","post_room_state":"3"} . Nevermind that the daterangepicker.js returns dates in the hideous MM/DD/YYYY format, which I would very much like to change to YYYY-MM-DD . The real problem is, the code never gets to success: function() .

Now my sql.php is in the same folder as calendar.js and index.php .

In sql.php I try to retrieve those values with:

$currentDateRange = $_REQUEST['post_date_range'];
$currentRoomNumber = intval($_REQUEST['post_room_number']);
$currentRoomState = intval($_REQUEST['post_room_state']);

I have checked many other SO Q&As and none have helped me solve my problem. I don't see any spelling errors. It's not disobeying same origin policy rule. I don't want to use jQuery $.post function. Anyone sees the obvious solution?

您想发送数组而不是字符串,所以直接发送myData以获取PHP文件中的数组值,而不是转换为JSON字符串。它将根据需要与当前的PHP文件一起使用。

You should specify a POST key for the JSON data string you are sending:

var myJSON = JSON.stringify(myData);
(...)
$.ajax({
    (...)
    data: 'data=' + myJSON,

You need to parse (decode) this string in your PHP file to be able to use it as an array/object again:

$data = json_decode($_REQUEST['data']);
$currentDateRange = $data['post_date_range'];
$currentRoomNumber = intval($data['post_room_number']);
$currentRoomState = intval($data['post_room_state']);

Also, dataType in jQuery.ajax function is specified as "The type of data that you're expecting back from the server." according to jQuery documentation . As far as I can tell from your code, you might rather expect something else as your response, so try excluding this line.

I am sorry to have burdened you all. It's my third week programming so my lack of knowledge is at fault. I did not know how dataflow works between AJAX and PHP in the URL...

While I was searching for other errors, I printed or echoed out many different things. The PHP file should echo only 1 thing, although it can be an array with multiple values.

Like this for example:

$return_arr = array("sql"=>$sql1, "result"=>$result, "out"=>$out);
echo json_encode($return_arr);

I appologize again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM