0

我创建了 jquery 选项卡,并且在选项卡内部有一个表单,在它的底部有这个按钮。

<button id="submitForm" onclick="formSubmition();">Submit</button>

这是标题

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="js/lib.js"></script>

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" href="css/style.css" />

formSubmition() 存在于 lib.js 文件中,这是它的代码

function formSubmition(){

    $recId              = $("#recId").val();
    $collectionDate     = $("#collectionDate").val();
    $collectorsName     = $("#collectorsName").val();
    $donorsName         = $("#donorsName").val();
    $sciName            = $("#sciName").val();
    $family             = $("#family").val();
    $comName            = $("#comName").val();
    $variety            = $("#variety").val();
    $area               = $("#area").val();
    $location           = $("#location").val();
    $altitude           = $("#altitude").val();
    $geoOrientation     = $("#geoOrientation").val();
    $geoCoordinates     = $("#geoCoordinates").val();
    $soilDescription    = $("#soilDescription").val();
    $habitatDescription = $("#habitatDescription").val();
    $plantPopulation    = $("#plantPopulation").val();
    $photoDate          = $("#photoDate").val();
    $photoId            = $("#photoId").val();
    $photoComments      = $("#photoComments").val();    
    $soilTaken          = 0;
    $seedQuantity       = $("#seedQuantity").val();
    $plantQuantity      = $("#plantQuantity").val();
    $graftQuantity      = $("#graftQuantity").val();
    $moreInfo           = $("#moreInfo").val();

    if($('#soilTaken').is(':checked'))          // Checking if Checkbox was checked.
        $soilTaken  = 1;


    jQuery.ajax({
        type: 'POST',
        async: true,
        url: 'classes/classController.php',
        data: {method: "recNewPlant", recId: $recId, collectionDate: $collectionDate, collectorsName: $collectorsName, donorsName: $donorsName, sciName: $sciName, family: $family, comName: $comName, variety: $variety, area: $area, location: $location, altitude: $altitude, geoOrientation: $geoOrientation, geoCoordinates: $geoCoordinates, soilDescription: $soilDescription, habitatDescription: $habitatDescription, plantPopulation: $plantPopulation, photoDate: $photoDate, photoId: $photoId, photoComments: $photoComments, soilTaken: $soilTaken, seedQuantity: $seedQuantity, plantQuantity: $plantQuantity, graftQuantity: $graftQuantity, moreInfo: $moreInfo},
        contentType: ('application/x-www-form-urlencoded'),
        dataType: "json",
        success : function(json){

          if(json.status == '1'){
              alert("Added!");
          }else{
            alert("Problema!");
          }
        }
    });

}

问题是我已经多次使用这种方法,但这是第一次出现这样的行为。我的意思是说。如果我按下按钮,大多数时候什么都不会发生,有时(很少)仅在铬(不在 Firefox 中)它向服务器发送请求并返回“问题”消息。

我已经删除了两个浏览器的缓存,但没有任何改变。此外,我正在使用 OWASP ZAP 查看流量,并且确实没有发送任何 POST 请求。我认为标签可能会导致该问题,但我找不到原因。

这是我设置标签的方式:

$("#mainTabs").tabs();

4

3 回答 3

0

没有必要检查

if(json.status == '1'){
              alert("Added!");
          }else{
            alert("Problema!");
          }

仅当请求成功时才执行成功函数()

尝试这个

如果要检查 ajax 调用中的一些错误,我添加了一个error:function(),如果发生错误会提示错误

function formSubmition(){

    $recId              = $("#recId").val();
    $collectionDate     = $("#collectionDate").val();
    $collectorsName     = $("#collectorsName").val();
    $donorsName         = $("#donorsName").val();
    $sciName            = $("#sciName").val();
    $family             = $("#family").val();
    $comName            = $("#comName").val();
    $variety            = $("#variety").val();
    $area               = $("#area").val();
    $location           = $("#location").val();
    $altitude           = $("#altitude").val();
    $geoOrientation     = $("#geoOrientation").val();
    $geoCoordinates     = $("#geoCoordinates").val();
    $soilDescription    = $("#soilDescription").val();
    $habitatDescription = $("#habitatDescription").val();
    $plantPopulation    = $("#plantPopulation").val();
    $photoDate          = $("#photoDate").val();
    $photoId            = $("#photoId").val();
    $photoComments      = $("#photoComments").val();    
    $soilTaken          = 0;
    $seedQuantity       = $("#seedQuantity").val();
    $plantQuantity      = $("#plantQuantity").val();
    $graftQuantity      = $("#graftQuantity").val();
    $moreInfo           = $("#moreInfo").val();

    if($('#soilTaken').is(':checked'))          // Checking if Checkbox was checked.
        $soilTaken  = 1;


    jQuery.ajax({
        type: 'POST',
        async: true,
        url: 'classes/classController.php',
        data: {method: "recNewPlant", recId: $recId, collectionDate: $collectionDate, collectorsName: $collectorsName, donorsName: $donorsName, sciName: $sciName, family: $family, comName: $comName, variety: $variety, area: $area, location: $location, altitude: $altitude, geoOrientation: $geoOrientation, geoCoordinates: $geoCoordinates, soilDescription: $soilDescription, habitatDescription: $habitatDescription, plantPopulation: $plantPopulation, photoDate: $photoDate, photoId: $photoId, photoComments: $photoComments, soilTaken: $soilTaken, seedQuantity: $seedQuantity, plantQuantity: $plantQuantity, graftQuantity: $graftQuantity, moreInfo: $moreInfo},
        contentType: ('application/x-www-form-urlencoded'),
        dataType: "json",
        success : function(json){
             alert(success);
        },
      error: function(jqXHR, textStatus, errorThrown) {
         alert(textStatus, errorThrown);
      }
    });

}

希望这有帮助,谢谢

于 2013-09-01T11:26:04.520 回答
0

这项工作,请确保您的 json 格式正确。

$(document).ready(function() {
  $('#form').submit(function(e) {
    e.preventDefault();
    $recId              = $("#recId").val();
    jQuery.ajax({
        dataType: 'json',
        type: 'POST',
        async: true,
        url: 'index.php',
        data: {method: "recNewPlant", recId: $recId},
        dataType: "json",
    })
    .done(function(json) {console.debug(json);})
    .fail(function() {console.debug('problema');}); 
    return true;
  });
});

php:

header('Content-type: application/json');
echo json_encode(array('status' => 1));
于 2013-09-01T11:57:53.797 回答
0

是的。这就是问题所在。不得不async:true换成async:false

于 2013-09-01T18:57:58.180 回答