Yii2 jQuery not working in IE11

2019-08-12 16:37发布

问题:

The Problem

I'm trying to create a page that updates a specific records field in a table when a checkbox is clicked (ideally without refreshing the page). I thought jQuery/AJAX would work for me.
Apparently not.

I've been struggling with this for a while, so I decided to scale it back and really simplify it to figure out what's not working. The updating of the table is fine (from Chrome), the not-refreshing isn't. And none of it works from IE11.

It's currently hosted on my local machine, so part of me is wondering if that's got something to do with it, however jQuery works fine on other sites I have hosted in the same way.

The Yii2 controller action:

public function actionTest() {
    return 'This is just a static test';
}

The (very basic) php page: This is the source after the page has been rendered

<script src="/assets/ddb27309/jquery.js"></script>
<script src="/assets/eeb0e590/yii.js"></script>

<div id="loadToMe"> </div>

<script type="text/javascript">
    $(document).ready( function() {
        $('#loadToMe').load('/mycontroller/testing');
    });
</script>

Now normally I'd expect the currently empty div to be populated with the string This is just a static test - nice and easy.

However.

Chrome & FireFox: Redirect to {mysite}/mycontroller/testing and shows the given text.

IE 11: gives me the following error message, but nothing else happens:

XMLHttpRequest: Network Error 0x2f76, Could not complete the operation due to error 00002f76.

This behaviour is identical for both browsers when I use $.get, $.post and $.ajax (with their various syntax differences) with one exception - IE11 gives a different error message for a POST.

XMLHttpRequest: Network Error 0x800c0008, The download of the specified resource has failed.

I've tried various things from various sources, including (but not limited to - I can't remember everything I've tried) the following:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />

<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />

AddDefaultCharset utf-8

And using the network tab from IE developer tools I have this for a POST and a GET respectively.

Protocol    Method  Result      Type        Received    Taken   Initiator       Wait    Start   Request Response    Cache read  Gap‎‎
HTTP        POST    302         text/html   464 B       47 ms   XMLHttpRequest  0       16      0       31          0           2902
HTTP                (Aborted)   text/html   506 B       < 1 ms  XMLHttpRequest  2902    0       0       0           0           47

The linked js files are ones that came from Yii2 (and are the minimum required in order for the $.load to work in Chrome) - I have not touched them in any way.

jQuery version is v2.1.3
Yii2 version is v2.0.3

回答1:

IE11 return Error:

XMLHttpRequest: Network Error 0x800c0008, The download of the specified resource has failed.

My solution: Yii2 controller SET action:

use yii\helpers\Json;

public function actionTest() {
    $out['message'] = 'This is just a static test';
    echo Json::encode($out);
    \Yii::$app->end();
}

OR

public function actionTest() {
        echo 'This is just a static test';
        \Yii::$app->end();
    }

Remove from Controller (PHP-code) all redirects and returns. ($this->redirect("..."))



回答2:

So it turns out that I have localeUrls and that was the problem!

My urls are prepended with the current language (i.e. myurl.com/en/controller/action)

When I was trying to load /mycontroller/testing it appears it was being redirected by the UrlManager to /en/mycontroller/testing, hence the redirection and not loading (at least that's what I'm understanding from it - I'm relatively new to Yii2)

If anyone can point me to somewhere that will explain this in more detail I'd be grateful, but at least now I can crack on with my project.

Thanks



回答3:

我重载了下 Response 类 (I overloaded the next class Response)

class Response extends \yii\web\Response{
  public function redirect($url, $statusCode = 302, $checkAjax = true)
  {
    if ($checkAjax && (Yii::$app->getRequest()->getIsPjax() || Yii::$app->getRequest()->getIsAjax())) 
    {
        $statusCode = 200;
    }
    return parent::redirect($url, $statusCode, $checkAjax);
  }
}


回答4:

Try status code of 200:

return $this->redirect(['linker/checkout'], 200);