首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >window.location.href未刷新/重新加载页面

window.location.href未刷新/重新加载页面
EN

Stack Overflow用户
提问于 2019-09-16 20:45:42
回答 1查看 1.6K关注 0票数 1

创建一个refreshcart()函数,并在函数中将window.location.href分配给currentUrl。但是在else块window.location.href = currentUrl中进行ajax调用时,为true;它不会刷新页面。

代码语言:javascript
复制
$(document).ready(function() {
  var productForm = $(".form-product-ajax")

  productForm.submit(function(event) {
    event.preventDefault();
    //console.log("Form is not sending!")
    var thisForm = $(this)
    //var actionEndpoint = thisForm.attr("action");
    var actionEndpoint = thisForm.attr("data-endpoint");
    var httpMethod = thisForm.attr("method");
    var formData = thisForm.serialize();

    $.ajax({
      url: actionEndpoint,
      method: httpMethod,
      data: formData,
      success: function(data) {
        console.log("success: ", data)
        //currentPath = window.location.href --if cart is in current path--
        if (window.location.href.indexOf("cart") != -1) {
          refreshCart()
        }
      },
      error: function(errorData) {
        console.log("error: ", errorData)
      }
    })
  })

  function refreshCart() {
    console.log("In current cart")

    var currentUrl = window.location.href


    var refreshCartUrl = '/api/cart/'
    var refreshCartmethod = "GET";
    var data = {};

    $.ajax({
      url: refreshCartUrl,
      method: refreshCartmethod,
      data: data,
      success: function(data) {
        console.log("success: ", data)
        console.log("If block starts")
        if ([data.products].length > 0) {
          console.log("Condition checked")
        } else {
          console.log("It came here")
          window.location.href = currentUrl, true;
        }
      },
      error: function(errorData) {
        console.log("Oh no! error: ", errorData)
      }
    })
  }
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-16 21:12:40

要重新加载页面,只需使用

代码语言:javascript
复制
window.location.reload()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57957195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档