$ .post永远不会收到回复

时间:2018-02-25 13:11:11

标签: javascript php jquery post

我是一个相当缺乏经验的编码员,我正在寻求帮助,说明为什么我没有收到对我的$ .post命令的回复。

从输出中,(我认为)帖子正确地提交了PHP页面,并且PHP正确地创建了具有我期望的值的JSON文件。问题是我的回调似乎永远不会发生。

我从未收到“功能响应”的日志消息,因此,我认为该帖子不会进入回调。

我已经阅读了很多,并尝试了一堆解决方案,包括一些AJAX。但大约10个小时后,我很难过。我的$ .post基于本指南:Save JavaScript variables to PHP/MySQL DataBase Securely with Ajax Post

感谢您提供任何帮助。

我正在Windows最新的WAMP服务器上测试代码。

index.php(相关位)

$('#radarDropdown').change(function () {
    currentRadarId = $('#radarDropdown').val();
    var radSel = document.getElementById('radarDropdown');
    var currentRadarName = radSel.options[radSel.selectedIndex].text;
    document.getElementById('radarSelectedLabel').innerHTML = currentRadarId;
    document.getElementById('radarSelectedName').innerHTML = currentRadarName;
    getBacks(currentRadarName, processResponse);
//  getBackground(currentRadarName);
    console.log('Start request');
                
//  document.getElementById('returnBackground2').innerHTML = back1;
//     get background image filename for this radar.
});

function getBacks(currentRadarId, callbackFn) {
    console.log('Enter getBacks');
    $.post(
            "getBackgrounds.php",
            {radarBOMId: currentRadarId},
            function(response) {
              console.log('function response');
              processResponse(response);
            },'json');
};
          
function processResponse(response){
    console.log('Entered processResponse');
    console.log(response);
    var backgroundFile = response.background;
    var locationsFile = response.locations;
    var roadsFile = response.roads;
    var riversFile = response.riverBasins;
    var railFile = response.rail;
    var rangeFile = response.range;
    var topoFile = response.topography;
    var catchFile = response.catchments;
    var wthrDistrictsFile = response.wthrDistricts;
    var waterwaysFile = response.waterways;
    document.getElementById('returnBackground2').innerHTML = backgroundFile;    
    };
});

getBackrounds.php:

<?php
header('Content-type: application/json');

require_once('dbconnect.php');

$typesArray = array(
        'background',
        'catchments',
        'locations',
        'rail',
        'range',
        'riverBasins',
        'roads',
        'topography',
        'waterways',
        'wthrDistricts',
    );

$idval = mysqli_real_escape_string($connection, $_POST['radarBOMId']);

foreach ($typesArray as $i => $value) {
    $sql = 'SELECT backfilename, backtype FROM InUseRadarsBackgroundsView WHERE productidbom ="'. $idval. '" and backtype = "'.$value.'"';
    $result = $connection->query($sql);
    
    $response = array();
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $response[$value] = $row["backfilename"];
            //console.log('Processed row ' & $i);
    }
    echo json_encode($response);
    } else {
        echo "  0 results";
    }
}
?>

POST回复:

{"background":"IDR503.background.png"}{"catchments":"IDR503.catchments.png"}{"locations":"IDR503.locations.png"}{"rail":"IDR503.rail.png"}{"range":"IDR503.range.png"}{"riverBasins":"IDR503.riverBasins.png"}{"roads":"IDR503.roads.png"}{"topography":"IDR503.topography.png"}{"waterways":"IDR503.waterways.png"}{"wthrDistricts":"IDR503.wthrDistricts.png"}

1 个答案:

答案 0 :(得分:0)

这不是完整的答案,但尝试添加其他回调(例如,失败回调)以检查进行此POST时是否发生某些错误。以下是如何完成此操作的示例:

$.post( "example.php", function(data) {
  alert( "success" );
})
.fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus );
})
.always(function() {
  alert( "finished" );
});