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);
});
Instead of doing all this you can rely on $this->input->is_ajax_request()
from http://codeigniter.com/user_guide/libraries/input.html. If you are not interested in loading a library, here is somewhat similar code I have in production for last two years at least.
$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest')? true : false;
Look at the string, its XMLHttpRequest
and the jQuery is the frontend JS toolkit
Just to add more, I usually have one entry point for Ajax calls, so obviously I have one controller for ajax and route all my calls through it. Why would I do that? Simple reason, I make sure all my forms submit to a simple non ajax form handler at server if JS is turned off. If JS is on, jQuery/prototype/YUI takes control and sends data to my ajax controller. The end handler which actually does all the validation/verification/db interaction is common code.
case $method == 'IS_AJAX':
Your $method
is not IS_AJAX
with this url:
http://localhost/2fb/index.php/redirect
This would bring you to the redirect
controller without a method (will default to "index"). You literally would need:
http://localhost/2fb/index.php/redirect/IS_AJAX
...to step into that case
. You seem to be confusing your constant IS_AJAX
with the method requested, which you seem to be using correctly when checking for index
(although this is the same as the default
case, 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:
switch ($method) {
// case $method === 'index':
case 'index':
$this->load->view('main');
break;
}
try this ,
$.ajax({
type: "POST",
url: "your function url goes here",
data: data,
success: function(html){
window.location = 'redirect url goes here';
}
});
精彩评论