title: how to redirect to another page if condition is not valid while using ajax?
body: <p>I want add product to the cart through ajax. only logged in user can add product to the the user. if user is not logged in redirect him to the log in page</p>
  <blockquote>
  <p>Here are my ajax request in blade template</p>
  </blockquote>
  <pre><code>    $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name=&quot;csrf-token&quot;]').attr('content')
          }
      });
  
      function addedToCart(){
          var product = $(&quot;#productId&quot;).val();;
          var val = $(&quot;#countItem&quot;).val();
          var unit = parseInt(val);
          $.ajax({
              type: &quot;POST&quot;,
              url: &quot;/addtocart&quot;,
              data: {product: product, unit: unit},
              dataType: &quot;json&quot;,
              success: function (res){
                  alertMsg.fire({
                      title: 'Product added to Cart'
             })
              }
          });
      }
       `
  </code></pre>
  <blockquote>
  <p>Here the controller code</p>
  </blockquote>
  <pre><code>function addToCart(Request $req){
      if($req-&gt;session()-&gt;has('user')){
          $cart = new Cart;
          $cart-&gt;user_id = $req-&gt;session()-&gt;get('user')['id'];
          $cart-&gt;product_id = $req-&gt;product;
          $cart-&gt;unit = $req-&gt;unit;
          $cart-&gt;save();
          return response($cart, 201);
      }
      
      else{
          return redirect('/login');
      }
      
  }
  </code></pre>
  <p><strong>It can not go the login route still remain in the same page</strong></p>