0

I am trying to call a controller function using ajax after clicking a button in a view. I am using Bonfire but there is no response at all. I need guidance. Below is my JQuery/Ajax code placed in a js file in the assets folder of one of my modules.

var base_url = '<? base_url()?>';
$('.tes_btn').click(function(){

     var html_data = $(".admin-box").html();

        $.ajax({
             url: base_url + "test_module/reports/domPdfTest",  
             type: "POST", 
             data:"html_data="+ html_data,
             success:function(result){   
           // alert('Good work');
        }
    });
}); 

the controller function looks like this

function domPdfTest(){

    $dompdf = new DOMPDF();
    $html = $this->input->post('html_data');
    $this->dompdf->load_html($html);
    $this->dompdf->render();
    $this->dompdf->stream("test_report.pdf");
}

I Load the following in the constructor of my controller

$this->load->library('dompdf_gen');
Assets::add_module_js('test_module', 'test_module.js');

and my view button looks like this

<input type="button" id="tes_btn" value="tes btn" class="tes_btn"></input>

Is there anything am missing? Thanks in advance..Judas

1 Answers1

0

Instead of this:

var base_url = '<? base_url()?>';

try this:

var base_url = "http://" + window.location.host + "/";

window.location.host holds the host except the http:// part.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • yeah, this is good. The url seems fine now as generated by console.log http://localhost/cash_crops_grown/reports/domPdfTest ...clicking the button shows a 404; POST http://localhost/cash_crops_grown/reports/domPdfTest 404 (Not Found) – Judas Tadeo Ateenyi Dec 27 '14 at 15:13
  • Can you describe the path of your domPdfTest file? Does it have an extension? Do you have some rewrite rules in .htaccess which enables this kind of access? – Lajos Arpad Dec 27 '14 at 15:52
  • oooh, domPdfTest is a function in a controller called reports (as described in the question). I am calling that function...the url should look like this http://localhost/lims/public/index.php/admin/reports/cash_crops_grown/domPdfTest – Judas Tadeo Ateenyi Dec 27 '14 at 16:17
  • Then this: test_module/reports/domPdfTest is not a valid url. How do you want to tell the server that you want to call that function? Through GET, or POST parameters? Or something else? – Lajos Arpad Dec 27 '14 at 17:29
  • Using POST parameter. In this case, cash_crops_grown is the module. Within domPdfTest I have the line of code $html = $this->input->post('html_data'); which should capture posted data from ajax. The url is the main issue – Judas Tadeo Ateenyi Dec 27 '14 at 17:50
  • Then why don't you try it with: url: base_url + "lims/public/index.php/admin/reports/cash_crops_grown/domPdfTest". I am helping you based on the information you give me. If you describe your problem clearly, then it is easier for me to help you. – Lajos Arpad Dec 27 '14 at 18:18
  • I indeed appreciate your help. Testing it directly gives a 404; POST http://localhost/lims/public/index.php/admin/reports/lims/public/index.php/admin/reports/cash_crops_grown/domPdfTest 404 (Not Found)... is it possible we can connect on skype or use team viewer? am certain this will help alot..my skype username is jt-judas..thanks alot – Judas Tadeo Ateenyi Dec 27 '14 at 19:00
  • I am in the middle of something, I cannot chat right now, but I believe you need to refer to http://localhost/lims/public/index.php/admin/reports/lims/public/index.php It seems that the additional content after the .php is the mistake and I believe you need to post to index.php and pass the needed post parameters inside data. – Lajos Arpad Dec 27 '14 at 19:30
  • Okay thanks for your time, I will keep trying and will post my final solution. – Judas Tadeo Ateenyi Dec 28 '14 at 16:25
  • You are welcome, best of luck with your problem and I wish you a happy new year – Lajos Arpad Dec 29 '14 at 09:23
  • Thanks, and happy new year to you too. – Judas Tadeo Ateenyi Dec 30 '14 at 10:01
  • the right URL is being obtained now using url: window.location + "/domPdfTest", ...looking at console, it generates Internal server error: POST http://localhost/lims/public/index.php/admin/reports/cash_crops_grown/domPdfTest [HTTP/1.1 500 Internal Server Error 15ms]...any advice here? thanks in advance – Judas Tadeo Ateenyi Jan 02 '15 at 10:48