Welcome Guest, Not a member yet? Register   Sign In
Jquery ajax and CI not working
#1

[eluser]Edgars[/eluser]
My controller
Code:
class main extends Controller {
function __construct()
       {
            parent::Controller();
                $this->load->helper('url');
       }
    
    function index () {    
    $this->load->view('header');
    $this->load->view('home');
    }
    
    function doajax () {    
    echo "asd";    
    }
    }
Javascript
Code:
$(document).ready(function(){

$('#send').click(function() {

$.ajax({
   type: "POST",
   url: "/main/doajax",
   success: function(data){
     alert(data);
                          }
    });
});
});

It can't be any simpler and still not working.

I added this to constants.php config file. Do I need it anyway?
Code:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
#2

[eluser]SPeed_FANat1c[/eluser]
try using full url like www.domain.com/main/doajax, maybe there is a problem, those relative url's often cause errors for me.
#3

[eluser]Abdul Malik Ikhsan[/eluser]
first, you can define your base_url in top Smile
Code:
var base_url = '<?php echo base_url(); ?>';
so you can call
Code:
url: base_url+"main/doajax",
#4

[eluser]Edgars[/eluser]
[quote author="Abdul Malik Ikhsan" date="1290738342"]first, you can define your base_url in top Smile
Code:
var base_url = '<?php echo base_url(); ?>';
so you can call
Code:
url: base_url+"main/doajax",
[/quote]

Code:
var base_url = '<?php echo base_url(); ?>';

Can't do that, because It's javascript (no php in JS).
And I have seen tutorials where
Code:
url: "/main/doajax",
is allowed. Can't figure why I can't. When I remove whole CI and leave just 3 files(index.php, JS and .php which I use through JS) - it works. So I believe CI doesn't like it.
#5

[eluser]smilie[/eluser]
"Can’t do that, because It’s javascript (no php in JS)."

You mean, it is .js file?

Do you have Firefox? If so, install Firebug and load that page and call Ajax. Firebug should report POST and Response, which should tell you what's the problem. Specifically, look in the POST info, where does the Ajax request go to.

Cheers,
Smilie
#6

[eluser]cryogenix[/eluser]
yes you can by using:

Code:
header('Content-Type: text/javascript');

and then save your js file with a .php extension. and then naturally call your php (js) like a regular javascript file like:

Code:
[script type="text/javascript" src="yourphpjs.php"][/script]
#7

[eluser]nuwanda[/eluser]
Relative urls work just fine, and yours is constructed correctly.

But, check the obvious things. Is that click event actually firing? Put an alert after it and see.

Also, check your js syntax very carefully. I've seen IE fail to execute code that other browsers process without a hitch, and it's usually syntax.

Yes, use firebug to check headers to see if the ajax is communicating with the server.

The ajax header constant is not required. It would be if you needed to test if the request to your method was via ajax or a regular page request. Not needed in this case.
#8

[eluser]nuwanda[/eluser]
I've recently replied to this thread. It may help you:

http://ellislab.com/forums/viewreply/828298/
#9

[eluser]cideveloper[/eluser]
So this is what I have and it works fine. Please show us your view "home" which is where you probably have your js loaded from and the item with id send. I have a feeling your external js file is not loading

Controller
Code:
<?php

class main extends Controller {

    function __construct()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->view('home');
    }

    function doajax()
    {    
        echo "asd";
    }
}
Javascript
Code:
$(function(){

$('#send').click(function() {

$.ajax({
    type: "POST",
    url: "/main/doajax",
    success: function(data){
        alert(data);
    }
    });

});    

});
View
Code:
<html>
<head>
<title>Welcome to CodeIgniter</title>
<p><br /><a href="#" id="send">send</a></p>
[<]script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"[>][<]/script[>]
[<]script type="text/javascript" src="assets/js/main.js"[>][<]/script[>]
&lt;/body&gt;
&lt;/html&gt;
#10

[eluser]Edgars[/eluser]
[quote author="smilie" date="1290749739"]"Can’t do that, because It’s javascript (no php in JS)."

You mean, it is .js file?

Do you have Firefox? If so, install Firebug and load that page and call Ajax. Firebug should report POST and Response, which should tell you what's the problem. Specifically, look in the POST info, where does the Ajax request go to.

Cheers,
Smilie[/quote]
I did and you were right - POST http://localhost/main/doajax 404 Not Found
So i changed from url: "/main/doajax" to url: "/index.php/main/doajax" and it worked, although I don't like that index.php in front.
Thanks for help. Smile




Theme © iAndrew 2016 - Forum software by © MyBB