简体   繁体   中英

Chrome does not execute js properly but firefox does it

Sorry for my english. Correct me if I wrong. This function execute properly in firefox but target browser is chrome and I dont know what's wrong

myjs.js

function addProduct(order_id) {
  $.ajax({
    type: "post",
    data: $("#formProducts").serialize(),
    url: "/save/" + order_id,
    complete: function(){}
  });
  $.ajax({
    type: "get",
    url: "/add/product/" + order_id,
    complete: function(){
      window.location.reload()
    },
  });
}

views.py

def post(self, request: HttpRequest, *args, **kwargs):
        order = Order.objects.get(id=int(kwargs['order_id']))
        try:
            comment = Comment.objects.get(order=order)
            comment.text = request.POST['comment']
        except Exception as _:
            comment = Comment(text="", order=order)
        comment.save()
        for key, val in request.POST.items():
            if "status" in key:
                idp = int(key.split("status")[1])
                product = Product.objects.get(id=idp)
                product.status = val
                product.save()
            elif "provider" in key:
                idp = int(key.split("provider")[1])
                product = Product.objects.get(id=idp)
                product.provider = val
                product.save()
            elif "med" in key:
                idp = int(key.split("med")[1])
                product = Product.objects.get(id=idp)
                product.name = val
                product.save()
            elif "amount" in key:
                idp = int(key.split("amount")[1])
                product = Product.objects.get(id=idp)
                product.amount = val
                product.save()
            elif "cost" in key:
                idp = int(key.split("cost")[1])
                product = Product.objects.get(id=idp)
                product.cost = val
                product.save()
            elif "ITOG" in key:
                order.summary = val
                order.save()
        url_ref = request.session["url_ref"]
        return redirect(url_ref)

def addProduct(request: HttpRequest, order_id):
    order = Order.objects.get(id=int(order_id))
    product = Product(name="", amount=1, cost=0.0, order=order)
    product.save()
    return HttpResponse()

Function must collect formdata and post it. Then make get request and reload page. Chrome does it but I don't see saved data after reload page, I have to reload page again and then I see save data.

Thanks halcyon. Your answer is right

You can use this structure

 $(document).on('click','.sendwork',function(e){
       $.ajax({
        type: "post",
        data: $("#formProducts").serialize(),
        url: "/save/" + order_id,
        complete: function(){}
      });
      $.ajax({
        type: "get",
        url: "/add/product/" + order_id,
        complete: function(){
        },
      });
      window.location.reload()
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM