0

I am using ASP.NET MVC 4 in VS2012.

I am using bundles to minify my JavaScript code.

My bundle code is:

bundles.Add(new ScriptBundle("~/bundles/inline").Include("~/Scripts/inline/_layout_inline.js"));

JavaScript source looks like this:

function requestPurchaseCompanies()
{ 
    var urlStr = '/Search/PurchaseSelectedCompanys';
    $.ajax({
        url: urlStr,
        dataType: 'json',
        type: 'POST',
        error: function (result, status, error) {
            var message = JSON.parse(result.responseText);
            parsePurchaseError(message);
        },
        success: function (data) {
            // get the result and do some magic with it
            var message = data.Message;
            if (window.location.href.indexOf('page=') < 0) {
                var url = '/Search/PagingDisplay';
                var params = '?page=1';
                window.location = 'http://' + window.location.host + url + params;
            } else {
                window.location = window.location;
            }
        }
    });
}

Note the line : error: function (result, status, error) {

Here is the rendered minified JavaScript:

function requestPurchaseCompany(n, t, i, r) {
    var u = undefined,
        f, e, o, s, h;
    n != null && i == "SearchResults" ? (f = $(n).closest("div.result")[0], f && f != "undefined" && (e = $(f).children("span[id=SelectDuns]")[0], e && e != "undefined" && (o = $(e).children("input:hidden")[0], o && o != "undefined" && (u = new CompanySelectState, u.CompanyID = o.value)))) : t != undefined && (u = new CompanySelectState, u.CompanyID = t), u && u != undefined && (s = "/Search/PurchaseCompany", h = JSON.stringify(u), $.ajax({
        url: s,
        type: "POST",
        dataType: "json",
        data: h,
        contentType: "application/json; charset=utf-8",
        error: function (n) {
            var r = JSON.parse(n.responseText);
            parsePurchaseError(r)
        },
        success: function (t) {
            var e = t.Message,
                u, f;
            t.State == !0 && $(n).attr("disabled", ""), n != null && i == "SearchResults" ? window.location.href.indexOf("page=") < 0 ? (u = "/Search/PagingDisplay", f = "?page=1", window.location = "http://" + window.location.host + u + f) : window.location = window.location : window.location = window.location, r != undefined && r == !0 && window.opener != undefined && window.opener.location != undefined && window.opener.location.reload()
        }

    }))
}

NOTE line 10: error:function(n){

So it started out as:

error: function (result, status, error) {

and got rendered as:

error:function(n){

The issue with this is that the minified code that has one parameter treats that parameter as the error parameter not the XMLHttpRequest that I require.

How do I fix this?

Nope
  • 22,147
  • 7
  • 47
  • 72
GregJF
  • 456
  • 4
  • 14
  • There is nothing wrong with the minified file, you are not using the `status` and `error` variables but only the `result` variable. I'm assuming the minification process was smart enough to notice that and didn't bother including them. Is your code not working when you are running it with the minified file? Do you get any errors or warnings in the debugger console? – Nope Apr 08 '13 at 06:51

1 Answers1

0

Minification is the process of removing all unnecessary characters from source code, without changing its functionality.

Your error function is defined like this:

error: function (result, status, error) {
    var message = JSON.parse(result.responseText);
    parsePurchaseError(message);
}

As you can see you are not actually using status and error but you are only using the result.

I would assume that when the minification process is removing unnecessary characters that it will include unused variables, hence you get:

error:function(n){
    var r=JSON.parse(n.responseText);
    parsePurchaseError(r)
}

As status and error are not used it would make sense for them to be removed during the minification process.


The issue with this is that the minified code that has one parameter treats that parameter as the error parameter not the XMLHttpRequest that I require.

That is not true as the order of parameters is:

Function(jqXHR jqXHR, String textStatus, String errorThrown)

Therefore when only a single parameter is specified it will be the jqXHR parameter.

Given the nature of JavaScript it will pass all 3 parameters in the expected order to the error callback but if you only have a single parameter specified it won't be throwing an error. You don't need to specify any parameters if you wouldn't be using them inside your error callback and the code still works.

The below code would also work just fine:

error: function () {
    console.log("We have an error");
}
Nope
  • 22,147
  • 7
  • 47
  • 72