jquery function not redirecting url

2019-09-16 22:51发布

问题:

I am working with codeigniter and jquery. I am using ajax to send some info to a codeigniter function to perform a db operation , in order to update the page. After the operation is complete I am trying to refresh the page. However the refresh works inconsistently and usually I have to reload the page manually. I see no errors in firebug:

           var message = $('#send_message').val()
               if ((searchIDs).length>0){
                   alert("searchIDs "+searchIDs );
               $.ajax({
                   type: "POST",
                   url: "AjaxController/update",
                   data:{ i : searchIDs, m : message },                        
                   dataType: 'json',
                   success: function(){
                       alert("OK");
                   },
                   complete: function() {
                        location.href = "pan_controller/my_detail";
                   }
               })
               .done(function() { // echo url in "/path/to/file" url
                // redirecting here if done
                alert("OK");
                location.href = "pan_controller/my_detail";
                });

                } else { alert("nothing checked") }
              break;

How can I fix this?

addendum: I tried changing to ;

  $.ajax({
                   type: "POST",
                   url: "AjaxController/update",
                   data:{ i : searchIDs, m : message },                        
                   dataType: 'json',
                   .done(function() { // echo url in "/path/to/file" url
                    // redirecting here if done
                        alert("REFRESHING..");
                        location.href = "pan_controller/my_detail";
                    });
                  }
               })

This is just defaulting to the website homepage. again, no errors in firebug

回答1:

Add the window object on location.href like this:

window.location.href = "pan_controller/my_detail";


回答2:

Try to use full path like

$.ajax({a
               type: "POST",
               url: "YOURBASEPATH/AjaxController/update",
               data:{ i : searchIDs, m : message },                        
               dataType: 'json',
               .done(function() { // echo url in "/path/to/file" url
                // redirecting here if done
                    alert("REFRESHING..");
                    location.href = "YOURBASEPATH/pan_controller/my_detail";
                });
              }
           })

BASEPATH should be like this "http://www.example.com"



回答3:

Try disabling the csrf_enabled (config/config.php) and trying it. If that works, then re-enable the protection and, instead of compiling data yourself, serialize the form; or, at least include the csrf hidden field codeigniter automatically adds. You can also use GET to avoid the CSRF protection, but that's least advisable of of the solutions.