title: AJAX POST request not working with XMLHttpRequest in Laravel
body: <p>I'm using XMLHttpRequest to avoid using JQuery and I wanna make an Ajax request to delete an object but I keep getting redirected back and getting FOUND (302) HTTP errors.</p>
  
  <p>This is my code: </p>
  
  <pre><code>   function deleteGuia(urlToSend) {
          var borra = confirm('¿Está seguro de borrar la guía?');
          if (!borra) {
              return;
          }
          var req = new XMLHttpRequest();
          var csrfToken = document.querySelector('meta[name="csrf-token"]').content;
          req.open("POST", urlToSend, true);
          req.setRequestHeader('X-CSRF-TOKEN', csrfToken);
          req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
          req.onreadystatechange = function () {
              if (req.readyState === 4) {
                  if (this.readyState === this.DONE) {
                      console.log(this.responseURL);
                  }
                  if (req.status != 200) {
                      var msg = JSON.parse(req.response);
                      alert(msg.error);
                  } else {
                      alert('Exitoso');
                  }
              }
          }
          var data = new FormData();
          var guia = "{{$guia ?? ''}}";
          var estado = "{{$tipo ?? ''}}";
          data.append("guia", guia);
          data.append("tipo", estado);
          req.send(data);
      }
        &lt;/script&gt;
  </code></pre>
  
  <p>This one's the controller function:</p>
  
  <pre><code>public function eliminarGuia(Request $request) {
          $request-&gt;validate([
              'guia' =&gt; 'required|numeric',
              'tipo' =&gt; 'required'
          ]);
          $guia = (int)$request-&gt;input('guia');
          $tipo = $request-&gt;input('tipo');
          \Log::info('Guia' . $guia . ' Tipo: '. $tipo);
          if (strtoupper($tipo) === 'ENTREGA'){
              $borra_guia = Guia::where('guia', $guia)-&gt;where('estado', $tipo)-&gt;delete();
              $borra_ciclos = Device::where('guia_recepcion', $guia)-&gt;delete();
              if(!$borra_guia) {
                  return response(400)-&gt;json(['error', 'La guía no se encontró.']);
              }
          } else if (strtoupper($tipo) === 'REVERSA') {
              $borra_guia = Guia::where('guia', $guia)-&gt;where('estado', $tipo)-&gt;delete();
              $devices = Device::where('guia_reversa', $guia)-&gt;get();
              if (!$borra_guia){
                  return response(400)-&gt;json(['error', 'La guía no se encontró.']);
              }
              foreach($devices as $device)
              {
                  if (!$device-&gt;fecha_recepcion) {
                      $device-&gt;delete();
                  } else {
                      $device-&gt;guia_reversa = 0;
                      $device-&gt;fecha_reversa = null;
                      $device-&gt;save();
                  }
              }
          } else {
              return response(400)-&gt;json(['error', 'La guía no se encontró.']);
          }
          return response(200);
  
      }
  </code></pre>
  
  <p>web.php</p>
  
  <pre><code>Route::post('borrar_guia', 'VistasController@eliminarGuia')-&gt;name('borrar_guia');
  </code></pre>
  
  <p>There's no redirection at all. Why might that be happening? I don't know what else to add. The controller should return a 200 code when it delets an existing object in the database but it's getting a 200 from the redirection.</p>