0

I am working with codeigniter and jquery ajax. I'm having some incosistencies b/w my app locally on wamp (working perfectly) and my deployed app (not so much). Once possible suggested fix is to convert ajax relative paths to absolute paths for ajax, so it looks like:

url: "YOURBASEPATH/AjaxController/update",
location.href = "YOURBASEPATH/pan_controller/my_detail";

Following Convert ajax relative paths to absolute paths with codeigniter I have changed my code to :

In your header section just add the following script.

<script type="text/javascript">
    var BASE_URL = "<?php echo base_url();?>";
</script>

$.ajax({a
               type: "POST",
               url: BASE_URL+"AjaxController/update",
               data:{ i : searchIDs, m : message },                        
               dataType: 'json',
               .done(function() { 
                    alert("REFRESHING..");
                    location.href = BASE_URL+"pan_controller/my_detail";
                });
              }
           })

I thought this would work but for some reason BASE_URL is not being recognized as a variable and instead of seeing for eg:

url: "YOURBASEPATH/AjaxController/update",

I see:

url:  BASE_URL+ "/AjaxController/update",

When I look at the source.

I'm not seeing any errors in my editor. Any ideas why this is not working?

edit:

   case "Set":
               var message = $('#send_message').val()
               if (searchIDs.length>0){
               console.log(BASE_URL+ "Update/update");

               $.ajax({
                   type: "POST",
                   url: BASE_URL+"Update/update",
                   data:{ i : searchIDs, m : message },                        
                   dataType: 'json',

                    .done(function() { 
                        alert("REFRESHING..");
                         location.href = BASE_URL+"pan_controller/detail";
                       });
               })


                } else { alert("nothing checked") }
              break;
Community
  • 1
  • 1
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 1
    javascript is ran client side - view source shows you the code, not its output. Try adding `console.log(BASE_URL+ "/AjaxController/update");` or if you ar not using a browser with a js console `alert(BASE_URL+ "/AjaxController/update");` and you should see the full url – Steve Nov 03 '14 at 14:47
  • I'm pretty sure `base_url()` returns `'YOURBASEPATH'`. Try and see what's generated by that line. – Sergiu Paraschiv Nov 03 '14 at 14:49
  • As said by @Steve, you are seeing what you should see in source for that line. Follow his advice, as well as look in the source for the place where you do the PHP echo and set `BASE_URL`. That should show you `var BASE_URL = "/YOURBASEPATH";` – JAAulde Nov 03 '14 at 14:49
  • Did you set `$config['base_url']= 'http://example.com/yourfolder';` at config.php?and there is a typo `$.ajax({a` remove `a` and please look here how to do javascript redirect http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript. – Shaiful Islam Nov 03 '14 at 15:46
  • another thing do not put `` this code to js file.put them inside a php file(view file) – Shaiful Islam Nov 03 '14 at 16:52
  • Ok guys, first thanks for looking at this. I've followed this and Steve, you are correct and console.log(BASE_URL+ "/AjaxController/update"); does work correctly. However When I make the changes I must have some of parenthesis error which I can't figure out. I'm going to put my full functions code above. would someone mind taking a look. – user1592380 Nov 03 '14 at 17:40

3 Answers3

1

I answered this question earlier. Here I strongly recommended you to do the following thing step by step:

  1. Update your base url in config.php at line 17.
  2. Open your header file view's header portion and put the following script before calling any custom jquery or javascript file:

    var BASE_URL = "<?php echo base_url();?>";
    
  3. Now in your custom jQuery file you will get the BASE_URL as variable.

But remember this you need to use only one header file for whole project.

If you face any further problem then please share with me your view file structure.

Ariful Islam
  • 7,639
  • 7
  • 36
  • 54
1

Your syntax is wrong. If you want a function to run after the ajax request, use the complete callback, and you should also utilize success and error callbacks to debug:

case "Set":
    var message = $('#send_message').val();
    if (searchIDs.length>0){
        console.log(BASE_URL+ "Update/update");

        $.ajax({
            type: "POST",
            url: BASE_URL+"Update/update",
            data:{ i : searchIDs, m : message },
            dataType: 'json',
            success: function(data) {
                console.log(data);
            },
            error: function(data) {
                console.log(data);
            }
            complete: function() {
                alert("REFRESHING..");
                location.href = BASE_URL+"pan_controller/detail";
            }
    })


    } else { 
        alert("nothing checked");
    }
break;
Steve
  • 20,703
  • 5
  • 41
  • 67
0

What's wrong with this?

url: "/AjaxController/update",
John Corry
  • 1,567
  • 12
  • 15
  • if your current page is `http://localhost/folder/controller` it will recognize as `http://localhost/AjaxController/update` but it should be `http://localhost/folder/AjaxController/update` – Shaiful Islam Nov 03 '14 at 15:56
  • if you set `$config['base_url'] = 'http://localhost/folder/';` it will return `'http://localhost/folder/';` – Shaiful Islam Nov 03 '14 at 16:02
  • So if you want it relative to the current page, you could use: `url: "./AjaxController/update",` This is starting to look like more of an app architecture problem than a "how do I resolve a URL in javascript?" problem. – John Corry Nov 03 '14 at 18:02
  • Thanks, I tried url: "/AjaxController/update", but it doesn't work. – user1592380 Nov 04 '14 at 13:33