Jquery ajax random error in Chrome only

2019-02-16 16:19发布

Summary of problem

After logging in with Chrome via Forms Authentication. The landing page as returnUrl will error my jQuery ajax without hitting the server. The status code = 0 and the message = "error". (Hardly useful). Having tried Firefox and not being able to replicate the problem, I am starting to think Chrome is the issue. Clearing caches, closing and reopening does not fix. The closest article i can find is this. jQuery Ajax - Status Code 0? However, the URL is relative, /Test

Another SO article: jquery ajax problem in chrome

Longer description

I am getting an error with jQuery ajax. It seems that immediately after logging in with forms authentication; the landing page (returnUrl), will JS error. Then after refreshing the page (F5), the script will work (mostly). Even if not using F5, navigating to the same page using a link will allow the JS to work (mostly). So straight after login is my main testing path.

Also, navigating in another tab to /Test action directly, works. The original tab still errors until i navigate or refresh it as mentioned.

I have had times where the navigating or f5 refresh does not stop the error. Thus the use of "mostly" in the opening paragraph.

After a day and a bit of not being able to find a solid reason through research, JS debugger, simplification of code and various scenarios; I need to request some help.

Javascript error feedback

The most I can gather is a status code of 0 and an error message of "error".

Software and versions

ASP.NET MVC 3
Chrome v28.0.1500.72 m
jQuery v1.8.3
AttributeRouting

With Chrome, I have also tried emptying the cache and CTRL+F5.

MVC
I have an action protected by an [Authorize(Roles = "Admin")] Disabling this attribute does appear to solve my problem and then later on, not. So, sorry if any earlier statements confused. lol, short on hair!

[GET("Test")]
public ActionResult Test()
{
    Console.WriteLine("Test");
    return new EmptyResult();
}

Any javascript script files are not behind a folder that is protected with forms authentication.

jQuery doc.ready

I have a jQuery error event which I have used to pause debugging on to see the contents of the objects.

//-- while I am using this method, I have also used the `error: ` 
//-- part of $.ajax with the same result.
    $(function ()
    {
        $(document).ajaxError(function (event, jqxhr, settings, exception)
        {
            Debug.Log("event: " + event);
            Debug.Log("jqxhr: " + jqxhr.responseText);
            Debug.Log("settings: " + settings);
            Debug.Log("exception: " + exception);
        });
    });

I have a jQuery method which loads using the following events. While the followig script looks like it would slam the server. There is timing logic to ensure it only hits every 5 seconds.

$(document).on('mousemove','*',function(e){ TestMethod(this,e,'mousemove');});

Javascript method

This is my ajax call which is as simple as I can make it.

   function TestMethod(sender, e, eventTrgger)
    {
        $.ajax(
        {
            type: "GET",
            url: '/Test',
            dataType: 'html',
            success: function (html)
            {
                //-- tested with nothing here.
            }
        });
    }

Fiddler

When the error occurs, Fiddler does not show a GET request, meaning the server is not hit. Also, I place a debugger breakpoint in the action confirms the action does not get hit.

Additional: Since the ajax call occurs every 5 seconds. While on the page and seeing status-0 and error-"error"; I clear the cache and without refreshing the page, chrome and the ajax request starts working. For the record, my chrome cache seetings are set to as little cache as I am allowed to configure.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-16 17:20

The issue appears to be a bug with how Chrome 28 handles caching for GET requests. Version 27 works fine, as do POST requests in version 28.

You can workaround the issue by setting the 'cache' property to false in your $.ajax() call.

查看更多
登录 后发表回答