dousa1630 2018-07-10 09:20
浏览 107

Laravel:Ajax POST请求执行POST一秒和第二个不想要的GET

Given this function to execute the POST request:

$.post('../CPaddVolo/',{
                  _token: $('#_token').val(),
                  data:values
                }).done(function(){
                  if (msg==='ok'){
                    swal("Volo aggiunto con successo!");


                  }else{
                    swal("Errore nella gestione della richiesta: riprovare.");
                  }
                })
                .fail(function(err){
                    swal("Aggiunta del volo fallita.");
                });

Whole HTML code and JS in the page:

Modulo voli

                      <div class="row">
                        <!--Voli presenti-->
                        <div class="col-md-6">
                          <label>Voli presenti</label>
                          @if (count($data[6])>0)
                            @foreach ($data[6] as $volo)
                            <form action="{{route('updateVolo',$data[6]->id)}}" enctype="multipart/form-data" method="post">
                              <label for="compagnia">
                                Compagnia di volo
                              </label>
                              <select class="form-control" id="newVoloidCompagniaFk" name="compagnia">
                                  @foreach ($data[11] as $compagnia)
                                    @if ($compagnia->id===$volo->idCompagniaFk)
                                      <option value="{{$compagnia->id}}" selected="selected">
                                        {{$compagnia->nome}}
                                      </option>
                                    @else
                                    <option value="{{$compagnia->id}}">
                                      {{$compagnia->nome}}
                                    </option>

                                    @endif
                                  @endforeach
                              </select>

                              <label for="dataoraPart">Data e ora di partenza (gg/mm/aaaa hh:mm)</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}dataoraPart" name="dataoraPart"/>

                              <label for="dataoraArrivo">Data e ora di arrivo (gg/mm/aaaa hh:mm)</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}dataoraArrivo" name="dataoraArrivo"/>

                              <label for="origine">Origine:</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}origine" name="origine" />

                              <label for="destinazione">Destinazione:</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}destinazione" name="destinazione" />

                              <label for="classe">Classe:</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}classe" name="classe" />

                              <label for="servizi">Servizi:</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}servizi" name="servizi" />

                              <label for="buy">Buy:</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}buy" name="buy" />
                              <label for="sell">Sell:</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}sell" name="sell" />
                              <label for="bar">BAR:</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}bar" name="bar" />

                              <label for="istruzioni">Istruzioni:</label>
                              <input type="text" class="form-control" id="volo{{$volo->id}}istruzioni" name="istruzioni" />

                              <label for="status">Stato:</label>
                              <select name="status" id="volo{{$volo->id}}status">
                                @foreach($data[12] as $status)
                                  @if ($status->id===$volo->status)
                                    <option value="{{$status->id}}" selected="selected">
                                        {{$status->nome}}
                                    </option>
                                  @else
                                    <option value="{{$status->id}}" >
                                        {{$status->nome}}
                                    </option>
                                  @endif
                                @endforeach
                              </select>

                              <input type="submit" class="btn btn-success" value="Modifica volo"/>
                            </form>
                            @endforeach
                          @else
                            <br/>

                          Nessun volo presente per la proposta cliente.
                          @endif
                        </div>
                        <!--Aggiunta di un nuovo volo-->
                        <div class="col-md-6">
                          <label>Aggiunta nuovo volo</label><Br/>


                          <label for="compagnia">
                            Compagnia di volo
                          </label>
                          <select class="form-control" id="newVoloidCompagniaFk" name="compagnia">
                            <option value="0">
                              Seleziona una compagnia di volo
                            </option>
                              @foreach ($data[11] as $compagnia)

                                <option value="{{$compagnia->id}}">
                                  {{$compagnia->nome}}
                                </option>


                              @endforeach
                          </select>

                          <label for="dataoraPart">Data e ora di partenza (gg/mm/aaaa hh:mm)</label>
                          <input type="text" class="form-control" id="newVolodataoraPart" name="dataoraPart"/>

                          <label for="dataoraArrivo">Data e ora di arrivo (gg/mm/aaaa hh:mm)</label>
                          <input type="text" class="form-control" id="newVolodataoraArrivo" name="dataoraArrivo"/>

                          <label for="origine">Origine:</label>
                          <input type="text" class="form-control" id="newVoloorigine" name="origine" />

                          <label for="destinazione">Destinazione:</label>
                          <input type="text" class="form-control" id="newVolodestinazione" name="destinazione" />

                          <label for="classe">Classe:</label>
                          <input type="text" class="form-control" id="newVoloclasse" name="classe" />

                          <label for="servizi">Servizi:</label>
                          <input type="text" class="form-control" id="newVoloservizi" name="servizi" />

                          <label for="buy">Buy:</label>
                          <input type="text" class="form-control" id="newVolobuy" name="buy" />
                          <label for="sell">Sell:</label>
                          <input type="text" class="form-control" id="newVolosell" name="sell" />
                          <label for="bar">BAR:</label>
                          <input type="text" class="form-control" id="newVolobar" name="bar" />

                          <label for="istruzioni">Istruzioni:</label>
                          <input type="text" class="form-control" id="newVoloistruzioni" name="istruzioni" />

                          <label for="status">Stato:</label>
                          <select class="form-control" name="status" id="newVolostatus">
                            <option value="0">
                              Seleziona uno stato
                            </option>
                            @foreach($data[12] as $status)

                                <option value="{{$status->id}}" >
                                    {{$status->nome}}
                                </option>

                            @endforeach
                          </select>
                          <br/>
                          <br/>
                          <button class="btn btn-warning" onclick="addVolo()">Aggiungi nuovo volo</button>




                        </div>
<script type="text/javascript">
  $(document).ready(function() {
    $('#testo').summernote();

  });


function proposalDelete(id){
  swal("Sei sicuro di voler eliminare questa proposta cliente?",{
    buttons:{
      cancel: "Annulla",
      conferma: "Conferma"

    }
  })
  .then((value)=>{
    switch(value){
      case "cancel":
        swal("Operazione annullata");
        break;
      case "conferma":{

        $.ajax({
             type: "GET",
             url: '../CPDelete/'+id

         }).done(function( msg ) {
             if (msg==='ok'){
               swal("Proposta cliente eliminata.");

               window.location='../clientProposals';
             }else{
               swal("Errore nella gestione della richiesta: riprovare.");
             }

         });
      };break;
    }
  });

}


(function() {
    document.getElementById('datainizio').value=document.getElementById('datainizio').value.substr(8,2)+'/'+document.getElementById('datainizio').value.substr(5,2)+'/'+document.getElementById('datainizio').value.substr(0,4);
    document.getElementById('datafine').value=document.getElementById('datafine').value.substr(8,2)+'/'+document.getElementById('datafine').value.substr(5,2)+'/'+document.getElementById('datafine').value.substr(0,4);

})();


function addVolo(){
  swal("Procedere all'aggiunta del nuovo volo?",{
    buttons:{
      cancel: "Annulla",
      conferma: "Conferma"

    }
  })
  .then((value)=>{
    switch(value){
      case "cancel":
        swal("Operazione annullata");
        break;
      case "conferma":{

        if ((isValidDatetime($('#newVolodataoraPart').val())) && (isValidDatetime($('#newVolodataoraArrivo').val()))){
          if (($('#newVoloidCompagniaFk').val()!=0) && ($('#newVolostatus').val()!=0)){
            values=[];
            $('*[id*=newVolo]:visible').each(function() {

                  if ( ($(this).attr('id')!='newVoloidCompagniaFk') || ($(this).attr('id')!='newVolostatus')){
                    if ( ($(this).attr('id')=='newVolobar') ||
                          ($(this).attr('id')=='newVolobuy') ||
                            ($(this).attr('id')=='newVolosell')
                        ){
                      if (!isNaN($(this).val())){
                        values.push($(this).val());
                      }

                    }
                    else if ( ($(this).attr('id')=='newVolodataoraPart') || ($(this).attr('id')=='newVolodataoraArrivo')){

                        values.push(datetimeComposer($(this).val()));

                    }
                    else{
                      if ($(this).val().length>0){
                        values.push($(this).val());
                      }
                    }

                  }

              });

              values.push($('#CPID').val());
              if (values.length!=13){
                swal("Accertati di aver riempito tutti i campi e che gli indici di BAR, buy e sell siano numerici.");
              }else{
                console.log("eseguo");
                $.post('../CPaddVolo/',{
                  _token: $('#_token').val(),
                  data:values
                }).done(function(){
                  if (msg==='ok'){
                    swal("Volo aggiunto con successo!");


                  }else{
                    swal("Errore nella gestione della richiesta: riprovare.");
                  }
                })
                .fail(function(err){
                    swal("Aggiunta del volo fallita.");
                });


              }





          }else{
            swal("Selezionare una compagnia di volo e uno stato del volo stesso.");
          }

        }else{
          swal("Gli orari di partenza e arrivo non hanno un formato corretto.");
        }


      };break;
    }
  });

}

function datetimeComposer(value){
  return value.substr(6,4)+'-'+value.substr(3,2)+'-'+value.substr(0,2)+' '+value.substr(11,5)+':00';
}
//Verifica che la data orario sia scritta in formato corretto
function isValidDatetime(value){
  data=value.substr(0,10);
  ora=value.substr(11,5);

  if (value.length==16){
    if (value.substr(0,10) != '' && value.substr(0,10).match(/^\d{1,2}\/\d{1,2}\/\d{4}$/)){
      if (value.substr(11,5)!='' && value.substr(11,5).match(/^\d{1,2}:\d{2}([ap]m)?$/)){
        return true;
      }
    }
  }

  return false;




}

And this routing into my "web.php":

Route::post('/cms/CPaddVolo/', 'DetailsClientProposalController@addVolo')->name('addVolo');

Will be executed the first POST request (with any response given):

first POST request result

Then, simultaneously, will be executed a second GET request that isn't done in any part of my code:

GET request not wanted

And this, will throws an error (because the routing doesn't allow a GET request):

GET http://amaka.demomasetek.online/cms/CPaddVolo 405 (Method Not Allowed)

Actually, I tried also to set in the POST the entire URL with same result, but this problem still persists, I thought meanwhile that the problem was the last "/" in the URL itself: same result with or without.

Same result using $.ajax and the the "POST" set as parameters inside the callback.

  • 写回答

1条回答 默认 最新

  • douduoquan2824 2018-07-10 14:07
    关注

    Okay, from my understanding you are trying to excute 2 route consecutively? First one is a POST request, while the second one is a GET request?

    //this is your first request using POST  
    $.post('../CPaddVolo/',{
                      _token: $('#_token').val(),
                      data:values
                    }).done(function(){
                      if (msg==='ok'){
                        secondCPaddVolo(); //call it in success function
                        swal("Volo aggiunto con successo!");
    
    
    
                      }else{
                        swal("Errore nella gestione della richiesta: riprovare.");
                      }
                    })
                    .fail(function(err){
                        swal("Aggiunta del volo fallita.");
                    });
    
    
    function secondCPaddVolo(){
      $.ajax({
      method: "GET",
      data: {yourdata},
      url: '../CPaddVolo/', //whatever your URL is
      successs: function(data){
        //return your data
      },
      error: function(data){
      //return
     }
    });
    
    
    }
    

    What you can do is call your second request in the success function of your first AJAX. I hope this is what you meant. If you want another approach, you can call the controller function of your second request inside the controller function of your first request.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)