This issue is very likely codeigniter specific.
I have a controller called redirect.php that redirects from and to views. This controller for the most part has one public _remap function that does all the redirecting with a case statement. Everything has been working great until I sent a $.POST from a view back to the controller. I want it to hit the _remap and look for the fact that the request is coming from AJAX then do it’s case.
I have a IS_AJAX constant I’m checking against.
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
but whenever I hit the page it’s always remapping to the default and sending my request to that page where it’s basically returning me that pages data back when I’m echoing and alerting the data to and fro.
Any insights?
for reference,
redirect.php (there is more code to define variables and 2 more cases but it’s not hitting those, it’s hitting ‘index’ / default)
public function _remap($method)
{
switch ($method) {
case $method == 'index':
$this->load->view('main');
break;
case $method == 'IS_AJAX':
var_dump($_POST);
break;
default:
$this->load->view('main');
}
}
tweetview.php (view loaded by redirect controller in another case within redirect.php, json_tweets send is a JSON variable)
//jquery
$.post("http://localhost/2fb/index.php/redirect", {'json_tweets': json_tweets},
function(data) {
alert(data);
});
Your
$methodis notIS_AJAXwith this url:This would bring you to the
redirectcontroller without a method (will default to “index”). You literally would need:…to step into that
case. You seem to be confusing your constantIS_AJAXwith the method requested, which you seem to be using correctly when checking forindex(although this is the same as thedefaultcase, so it’s redundant).$method, or whatever you name the first parameter in_remap(), will always be the routed controller function that is called.EDIT: I failed to mention this earlier, but the switch block evaluates the expression you pass to it, so there is no need to do the comparison manually. Example: