问题
I am using codeigniter in my application and have a jquery post function that works perfectly.
$.post('getticketstacktype', {ticketnumber:ticketnumber},function(result) {
alert(result);
}
I am then using the exact same post function on another edit page. this page is prepopulated with PHP and works correctly.
I use the same post to call my controller, I have checked that ticketnumber value is correct
$.post('getticketstacktype', {ticketnumber:ticketnumber},function(result) {
alert(result);
}
the controller function it calls is:
function getticketstacktype(){
if (isset($_POST['ticketnumber'])){
$ticketnumber = $_POST['ticketnumber'];
$data = $this->wos_model->getticketstacktype($ticketnumber);
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
}
now where this works correctly the result is '7' but instead, on my edit page it is returning my entire view page rendered:
so <html><head>......</html> hundereds of lines)
I cant figure out my why my entire page is being returned as a result to a jquery post when it is working in another view.
I have also verified that the post information from both views is the same with firebug.
Any advice? Thanks as always.
回答1:
This usually happens when a fatal error occurred in the backend. When a fatal error occurs on PHP, it constructs a page to displays some error message contained within a box. jQuery tries to interpret to a meaningful data but it fails. And if you see this response in Firebug or Google Chrome Dev tool you can actually see that the page can be rendered.
- Use either Firebug or Google Chrome Dev tool to track down the result sent back from the server
- Make sure there is no error in your backend side
回答2:
I think you need this:
In your header define in a script tag:
var base_url = '<?php echo base_url() ?>'; //here we keep the base url in a variable to use in our js files
Then include the js file
below the above line which is calling your ajax, so we can access the above variable.
Now your ajax function
needs to be changed like this:
$.post(base_url+'controller_name/getticketstacktype/', {ticketnumber:ticketnumber},function(result) { //here if you see the absolute path to the funciton is given
alert(result);
}
You also need to check your firebug's net panel
and console
for any errors if you got. best of luck. :D
来源:https://stackoverflow.com/questions/23128416/jquery-post-returning-rendered-html-page-rather-than-sql-result