【问题标题】:Browser hangs after AJAX complete (waiting response?)AJAX 完成后浏览器挂起(等待响应?)
【发布时间】:2017-09-26 09:41:42
【问题描述】:

并不是真的希望得到答案,但现在这 2 天都在苦苦挣扎,不知道发生了什么。

所以这里我做了什么:

通过 AJAX 上传文件并立即在后端开始处理它。

$.ajax({
  type : "post",
  cache: false,
  contentType: false,
  processData: false,
  url : formAction,
  data : formData
});

在后端我编写会话,其中存储当前进度,例如 500 of 10000...

然后我调用另一个 AJAX 来检查会话的状态,我每 1 秒调用一次这个函数,直到响应返回“完成”。

在我看来是合法的,但问题是在后端处理完成后,第一个 XHR 将状态从“待定”更改为 200 个浏览器冻结,看起来好像有什么东西在阻止它,比如另一个请求或某事。检查下面的屏幕。

所以它完成了,检查停止,状态更改为 200,除了浏览器冻结,尝试 Chrome、Firefox...浏览器建议我终止进程,但如果我等待一段时间它会刷新。

更新

所以我撒了一点谎,我再次尝试做同样的事情但没有 ajax 并看到类似的问题,后端处理完成后,浏览器等待响应(向后旋转)......所以这就是表弟挂了。

这是我的后端,它是 Laravel

foreach ($chunks as $rows)
        {
            $current_chunk++;

            if ( $current_chunk < $all_chunks ) {
                Session::put(['import_progress'=>'Importing ' . $current_chunk * 150 . ' of ' . $all_results]);
                Session::save();
            } elseif ( $current_chunk >= $all_chunks ) {
                Session::put(['import_progress'=>'done']);
            }

            foreach ($rows as $row)
            {
                // transakcija start
                DB::transaction(function() use ($row) {

                    $code = $row[0];
                    $gender = $row[1];
                    $brand_name = filter_var($row[2],FILTER_SANITIZE_STRING);
                    $category = $row[3];
                    $subcategory = $row[4];
                    $condition = $row[5];
                    $details = $row[6];
                    $composition = $row[7];
                    $materials = $row[8];
                    $colors = $row[9];
                    $patterns = $row[10];
                    $country = $row[11];
                    $size = $row[12];
                    $m_a = $row[13];
                    $m_b = $row[14];
                    $m_c = $row[15];
                    $m_d = $row[16];
                    $m_e = $row[17];
                    $m_f = $row[18];
                    $m_g = $row[19];

                    $product = new Product();
                    $product->code = $code;
                    $product->gender = $gender;
                    $product->size = $country . ' | ' . $size;
                    $product->condition = $condition;
                    $product->measurement_a = $m_a;
                    $product->measurement_b = $m_b;
                    $product->measurement_c = $m_c;
                    $product->measurement_d = $m_d;
                    $product->measurement_e = $m_e;
                    $product->measurement_f = $m_f;
                    $product->measurement_g = $m_g;
                    $product->save();

                    if ( empty($code) ) {
                        $bad_prods[] = $product->id;
                    }

                    if ( !empty( $brand_name ) ) {
                        $brand = Brand::firstOrCreate(['brand_name'=>$brand_name]);
                        $product_b = new ProductBrand();
                        $product_b->brand_id = $brand->id;
                        $product_b->product_id = $product->id;
                        $product_b->save();
                    }

                    if ( !empty( $materials ) ) {
                        $materials_exp = explode(',',$materials);

                        foreach ($materials_exp as $material) {
                            $material = Material::firstOrCreate(['material_name'=>trim($material)]);
                            $product_m = new ProductMaterial();
                            $product_m->material_id = $material->id;
                            $product_m->product_id = $product->id;
                            $product_m->save();
                        }
                    }

                    if ( !empty( $patterns ) ) {
                        $patterns_exp = explode('/',$patterns);
                        foreach ($patterns_exp as $pattern) {
                            $pattern = Pattern::firstOrCreate(['pattern_name'=>trim($pattern)]);
                            $product_p = new ProductPattern();
                            $product_p->pattern_id = $pattern->id;
                            $product_p->product_id = $product->id;
                            $product_p->save();
                        }
                    }

                    if ( !empty( $colors ) ) {
                        $colors_exp = explode('/',$colors);
                        foreach ($colors_exp as $color) {
                            $color = Color::firstOrCreate(['color_name'=>trim($color)]);
                            $product_c = new ProductColor();
                            $product_c->color_id = $color->id;
                            $product_c->product_id = $product->id;
                            $product_c->save();
                        }
                    }

                    $product_t = new ProductTranslation();
                    $product_t->product_id = $product->id;
                    $product_t->product_title = $brand_name;
                    $product_t->product_description = '<p>'.$details.'</p><p>'.$composition.'</p>';
                    $product_t->save();

                }); // end transakcija
            }
        }

        Session::put(['import_progress'=>'done']);
        Session::put(['bad_prods'=>$bad_prods]);

        return redirect()->route('admin.import');

我在这里检查状态

public function status()
    {
        return response()->json([
            'status' => Session::get('import_progress'),
            'bad_prods' => Session::get('bad_prods')
        ]);
    }

【问题讨论】:

  • 在代码的其余部分有 SetTimeout()SetInterval() 吗?在定期间隔后发布您正在调用该函数的部分。
  • 我有,但是没有检查状态也会发生同样的情况,所以这不是问题......
  • 发布你正在刷新/检查状态的sn-p
  • 添加了检查状态方法,非常简单,只需从我在处理方法中设置的会话中获取数据。

标签: javascript jquery ajax browser xmlhttprequest


【解决方案1】:

处理完成后,我输入了简单的 exit();在处理控制器结束时...不确定为什么这有效,而任何其他返回语句都无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2016-12-23
    • 2011-11-10
    • 2018-03-22
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多