-1

I'm not sure what I'm missing so thought I'd throw this up here. Below is an .ajaxComplete method that is not working correctly. I need to compare the URL the action is coming from with the URL from the action I want this code to be executed after.

This is an ASP MVC site, so I use the helper to grab the URL from the appropriate controller action. I then use the settings object to determine where the URL from the source of the current Ajax call (I have a couple on this page so need to use this to differentiate).

As you can see from the screen shot, both the url variable and settings.url contain the same value. I also set an alert to check for the length of each, and the alert comes back with a length of 35 for both.

However... this code routinely fails the if condition (I cannot get the alert("here") to fire). I'm relatively new to jQuery so I'm wondering if this is the correct way to compare two strings? FYI - I originally wrote the condition to be

if(settings.url == url)

however I could not get this to pass either.

//Tie completion of GetChannel Ajax call to execution of 
//getDrmTerritory instead of using .change event on 
//Market Segment drop down
$(document).ajaxComplete(function (event, xhr, settings) {
    var url = '@Url.Action("GetChannel", "AgentTranmsission")';
    alert(url.length + " " + settings.url.length);
    alert("about to check");
    if (String(settings.url) == String(url)) {
        alert("here");
        var channel = $.trim($('#channelName').text());
        if ($('#AlignmentM:radio').is(':checked')) {
            if ($('#MailingZip').val() != "" && $('#MailingState').val() != "" && channel != "") {
                getDrmTerritory($('#MailingZip').val(), $('#MailingState').val(), channel);
            } else if (channel != "") {
                $('#territoryName').text("");
                $('#Region').val("");
            }
        }
        else if ($('#AlignmentL:radio').is(':checked')) {
            if ($('#LocationZip').val() != "" && $('#LocationState').val() != "" && channel != "") {
                getDrmTerritory($('#LocationZip').val(), $('#LocationState').val(), channel);
            } else if (channel != "") {
                $('#territoryName').text("");
                $('#Region').val("");
            }
        }
    }
    alert("end");
});

enter image description here

EDIT

Here is a screen shot of (settings.url == url) evaluated in Chrome's console

enter image description here

and here is the alert that tests alert(url.length + " " + settings.url.length);

enter image description here

NealR
  • 10,189
  • 61
  • 159
  • 299

2 Answers2

4

There is a spelling mistakes in the URL

Please check it out

Transmission

Tranmsission

Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
0

Just a guess, but why don't you try checking them for whitespace... I'm not sure whether it would be counted as part of length.

I've had similar issues before and would be the next place I would check. This link might help: Replace multiple whitespaces with single whitespace in JavaScript string

Community
  • 1
  • 1
gmeluski
  • 58
  • 3
  • I did check for length and they came back as being equal. – NealR May 23 '13 at 16:42
  • Cool. My next guess would be hardcoding the value and seeing how that tests against `url` and `settings.url` to see which one is deviating from the expected string. – gmeluski May 23 '13 at 16:47
  • If i do a cut/paste of what is in the console up above for the `url` variable and compare that with `settings.url` it passes the condition. However, this is why I used the `String(settings.url) == String(url)` condition, and I'm not sure why that isn't working. – NealR May 23 '13 at 17:05