1

I have an AJAX.

   var id = "some_text";
   $.ajax({
        type        : "GET",  
        dataType    : "json",
        url         : "<?php echo site_url('con_atk/get_outlet'); ?>",  
        async       : false,
        success     : function(outlet){

           $.map(outlet, function (v) { 

              if(v.NamaOutlet == id){
                window.location.href = "page_a.php";
              }
              else{
                window.location.href = "page_b.php"; 
              }

           })
        }   
})

I'd like to check if the value of id is exist in the AJAX success call (JSON object type). If it exists, redirect to page A and if not redirect to page B. When I use alert on v.NamaOutlet the value is exist. But why am I not redirected to page_a.php?

edit

when I use alert(JSON.stringify(outlet));

[{"KodeOutlet_iBSM":"ACG","NamaOutlet":"Accounting"},{"KodeOutlet_iBSM":"BBG","NamaOutlet":"Business Banking"},{"KodeOutlet_iBSM":"CB I","NamaOutlet":"Corporate Banking I"},{"KodeOutlet_iBSM":"CB II","NamaOutlet":"Corporate Banking II"},{"KodeOutlet_iBSM":"CBT","NamaOutlet":"Corporate & Branch Transformation"},{"KodeOutlet_iBSM":"CCG","NamaOutlet":"Culture & Customer Care"},{"KodeOutlet_iBSM":"CHG","NamaOutlet":"Consumer Finance & Hajj"},{"KodeOutlet_iBSM":"CMG","NamaOutlet":"Commercial Banking"}
Vahn
  • 542
  • 1
  • 10
  • 25

5 Answers5

1

Setting window.location.href doesn't immediately terminate the current script - I'm not sure how reliable this is cross browser, but in my testing in Chrome if you set window.location.href more than once in the same script the browser navigates to the last value set before the JS function ends.

So in your case it would navigate to the result of testing the last item in your array, because your $.map() loop runs the if/else for every item in the array.

You could instead use your loop to set a flag to indicate whether the item was found anywhere in the array:

    success     : function(outlet){
       var idFound = false;
       $.each(outlet, function (v) { 
          if(v.NamaOutlet == id){
            idFound = true;
          }
       });
       window.location.href = idFound ? "page_a.php" : "page_b.php";
    }   

Note that I've used $.each(), because although $.map() would achieve the same result it doesn't really make sense because you're not really doing any mapping.

You can tidy this up and remove the need for a flag variable by using the array .some() method rather than $.each():

    success     : function(outlet){
       if (outlet.some(function(v) { return v.NamaOutlet == id; })) {
          window.location.href = "page_a.php";
       else {
          window.location.href = "page_b.php";
       }
    }
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

The only possible reason I could find here is that you might be using Internet Explorer and internet explorer does not really accept relative URLs as mentioned in one of the answers here window.location.href not working on IE

Rest all seems good. Hope this helps.

Community
  • 1
  • 1
The Cloud Guy
  • 963
  • 1
  • 8
  • 20
0

TRY IT:

            var id = "Business Banking";
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "<?php echo site_url('con_atk/get_outlet'); ?>",
                async: false,
                success: function (outlet) {

                    //[{"KodeOutlet_iBSM":"ACG","NamaOutlet":"Accounting"},{"KodeOutlet_iBSM":"BBG","NamaOutlet":"Business Banking"},{"KodeOutlet_iBSM":"CB I","NamaOutlet":"Corporate Banking I"},{"KodeOutlet_iBSM":"CB II","NamaOutlet":"Corporate Banking II"},{"KodeOutlet_iBSM":"CBT","NamaOutlet":"Corporate & Branch Transformation"},{"KodeOutlet_iBSM":"CCG","NamaOutlet":"Culture & Customer Care"},{"KodeOutlet_iBSM":"CHG","NamaOutlet":"Consumer Finance & Hajj"},{"KodeOutlet_iBSM":"CMG","NamaOutlet":"Commercial Banking"}]

                    var exist = false;
                    for (var i = 0; i < outlet.length; i++) {

                        if (outlet[i].NamaOutlet === id) {
                            exist = true;
                            break;
                        }


                    }
                    if (exist) {
                        window.location.href = "page_a.php";

                    } else {
                        window.location.href = "page_b.php";

                    }
                }
            });
toto
  • 1,180
  • 2
  • 13
  • 30
  • This works. but `var exist = true` should be `var exist;`. – Vahn Dec 15 '16 at 05:04
  • 2
    `var exist = false;` would make more sense than `undefined` as a default, though either would work. – nnnnnn Dec 15 '16 at 05:05
  • if I'd like to store `window.location.href = "page_a.php` to a variable `link`. How can I use it outside AJAX call? – Vahn Dec 15 '16 at 05:05
  • My error. Variable exist should be false on initialized. I updated it. – toto Dec 15 '16 at 05:09
  • Yes you can use this outside after ajax call , but you need use alway async=false. do you need that i update the example ? – toto Dec 15 '16 at 05:24
0

if else condition is not suitable for your situation , let your id is Commercial Banking ,the last array value of your given json response .So mapping json will be check first index with id ,so if not equal it redirected to page_b .But you have id that exist in json response so that condition is not true for your condition . You wanna check id is exist in outlet so that map json array and if it found redirected page , if not found write the redirect other page in next execute line....

   var id = "Commercial Banking"; // eg value
   $.ajax({
        type        : "GET",  
        dataType    : "json",
        url         : "<?php echo site_url('con_atk/get_outlet'); ?>",  
        async       : false,
        success     : function(outlet){

           $.map(outlet, function (v) {
              if(v.NamaOutlet == id){
                window.location.href = "page_a.php";
              }    
           });
           window.location.href = "page_b.php";                   
        }   
 });
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
0
 $('#btn').click(function () {
                $.ajax({
                    type: "GET",
                    url: '/Contrller/Action Name',
                    data: {},
                    contentType: "application/json;",
                    dataType: "json",
                    success: function (r) {
                        console.log(r);
                        if (r == true) {
                            $.ajax({
                                type: "GET",
                                url: '/Contrller/Action',
                                data: {  },
                                contentType: "application/json;",
                                dataType: "json",
                                success: function (r) {
                                    $("#btnclosem").trigger("click")
                                    var select = $("#Div");
                                    select.empty();
               select.append($('<option/>', {value: 0,text: "" }));  $.each(r.list, function (index, itemData) {
}
                            });
                            return false;

                        }
                        else {
                            alert("")
                            return false;

                        }
                    }
                });