处理HTTP重定向状态代码

时间:2019-04-04 11:26:20

标签: reactjs react-redux redux-observable

我正在尝试处理HTTP重定向状态代码(例如,会话超时时为302重定向),我不知道是否存在使用redux-observable处理特定响应代码的通用方法?我现在遇到的问题是浏览器遵循302响应中指定的位置,而我只能进入登录页面的后续200响应。我现在有一些技巧,可以在响应URL中检测到“登录”一词,并使用window.location对象重定向到登录页面。我必须在每部史诗中都这样做。

这就是我所拥有的:

<?php 
include("config.php");
$sql="SELECT * FROM inventory_details where status ='0' limit 0,100";  
$query=mysqli_query($conn, $sql);

    while($row=mysqli_fetch_assoc($query)){
    $id = $row['id'];
    $inventorybackground = $row['inventorybackground'];
    $inventorycolor = $row['inventorycolor'];
    $firm_name = $row['firm_name'];
    $position = $row['position'];
    $city = $row['city'];
    $catagory_name = $row['catagory_name'];
    $supplier = $row['supplier'];

    if (isset($_POST['update'])){
        $position=$_POST['position'];

        $update_query="UPDATE inventory_details SET position ='$position' WHERE id='$id'";
        $run_update=mysqli_query($conn, $update_query);
    }
  ?>

  <div class="container">
    <div class="row" style="background-color: <?php echo $inventorybackground; ?> !important; color: <?php echo $inventorycolor; ?>; padding-top: 10px;">
      <form class="form-horizontal" method="post" enctype="multipart/form-data">
        <div class="col-md-1 inventory-data">
          <input type="text" class="form-control" id="id" name="id" value="<?php echo $id;?>" disabled>
        </div>

        <div class="col-md-3 inventory-data">
          <input type="text" class="form-control" id="firm_name" name="firm_name" value="<?php echo $firm_name;?>" disabled>
        </div>

        <div class="col-md-1 inventory-data">
          <input type="text" class="form-control" id="position" name="position" value="<?php echo $position;?>">
        </div>

        <div class="col-md-1 inventory-data">
          <input type="text" class="form-control" id="city" name="city" value="<?php echo $city;?>" disabled>
        </div>

        <div class="col-md-2 inventory-data">
          <input type="text" class="form-control" id="catagory_name" name="catagory_name" value="<?php echo $catagory_name;?>" disabled>
        </div>

        <div class="col-md-2 inventory-data">
          <input type="text" class="form-control" id="supplier" name="supplier" value="<?php echo $supplier;?>" disabled>
        </div>

        <div class="col-md-2 inventory-data">
          <button type="submit" name="update" value="update" class="btn btn-primary">Filter</button>
        </div>
      </form>
    </div>
  </div>

  <?php } ?>

有人知道有什么更好的方法可以解决这个问题,而我不必为所有新的史诗重复吗?

1 个答案:

答案 0 :(得分:1)

ajax操作包装XMLHttpRequestXMLHttpRequest自动跟随重定向。虽然无法阻止重定向,但是可以检测到重定向。这是检测重定向的另一个示例:

export const getData = action$ =>
  action$.pipe(
    ofType(GET_DATA_REQUEST),
    mergeMap(action =>
      ajax(options).pipe(
        mergeMap(response => {
          // Navigate to login if the request was successful but redirected
          if (response.status >= 200 && response.status < 300 && response.responseURL !== options.url) {
            window.location.href = 'login'
            return empty()
          }

          return of(getDataSuccess(response.response))
        })
      )
    )
  )

如果要在多个史诗中重用此逻辑,只需将其导出为可重用函数即可:

export const ajaxWithLoginRedirect = options =>
  ajax(options).pipe(
    mergeMap(response => {
      // Navigate to login if the request was successful but redirected
      if (response.status >= 200 && response.status < 300 && response.responseURL !== options.url) {
        window.location.href = 'login'
        return empty()
      }

      // Return the raw response
      return of(response)
    })
  )

export const getData = action$ =>
  action$.pipe(
    ofType(GET_DATA_REQUEST),
    mergeMap(action =>
      ajaxWithLoginRedirect(options).pipe(
        // This is only called if we did not redirect
        map(response => getDataSuccess(response.response))
      )
    )
  )

请注意,fetch API 确实支持手动处理重定向(您返回的响应对象将具有3xx状态代码)。在XMLHttpRequestfetch之间需要权衡取舍,因此我将研究如果在应用程序中自动跟随重定向的 not 是更可取的。