EllisLab text mark
Advanced Search
     
jQuery $.post and CI
Posted: 01 March 2011 05:32 AM   [ Ignore ]
Joined: 2011-02-28
8 posts

Hi,

I have a problem on my web application with jQuery $.post.

My javascript is :

$.post("create-account"{ name"John"pass"test" },
   function(
data{
     alert(
"Data Loaded: " data);
   
})

and the Php

echo 'data received'

The Php is called and executed but, I can not get the response in my javascript

Can you help me ?

thanks :)

 
Posted: 01 March 2011 05:54 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

Search the forums this has been answered a 100 times!

CSRF cookie in the config.php file.

You have to pass the cookie value to jQuery

InsiteFX

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 01 March 2011 06:52 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-02-28
8 posts

I can not find $config[‘CSRF’] on my config.php file…

 
Posted: 01 March 2011 07:37 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-17
278 posts

What are your response headers for AJAX request in Firebug?

 
Posted: 01 March 2011 09:03 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-02-28
8 posts

Réponsevoir le code source
Set-Cookie   60gp=R152070632; path=/; expires=Fri, 04-Mar-2011 00:16:00 GMT
Date   Tue, 01 Mar 2011 11:59:41 GMT
Server   Apache/2.2.X (***)
X-Powered-By   PHP/5.3.5
Vary   Accept-Encoding
Content-Encoding   gzip
Content-Length   60
Keep-Alive   timeout=5, max=100
Connection   Keep-Alive
Content-Type   text/html
Requêtevoir le code source
Host   mysite*********
User-Agent   Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept   */*
Accept-Language   fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding   gzip,deflate
Accept-Charset   ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive   115
Connection   keep-alive
Content-Type   application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With   XMLHttpRequest
Referer   http://mysite*********
Content-Length   122
Cookie   60gp=R152070632; ci_session=a:4:{s:10:“session_id”;s:32:“d7d2cc52db790a529e47b77656bf7acd”;s:10:“ip_address”;s:13:”******”;s:10:“user_agent”;s:50:“Mozilla/5.0+(Windows;+U;+Windows+NT+6.1;+fr;+rv:1.”;s:13:“last_activity”;s:10:“1298980677”;}17fbb6b53141cc1cf003af586b186b33

Is it what you need ?
On firebug I see the response but javascript can not retrieve it

 
Posted: 01 March 2011 09:10 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-17
278 posts

Look like you’re getting your response back (Content-Length: 122). Try

$.post("create-account"{ name"John"pass"test" },
   function(
data{
     console
.log(data);
     
alert("Data Loaded: " data);
   
}); 

and check Firebug console for returned variable.

 
Posted: 01 March 2011 09:14 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-02-28
8 posts

Ok, I have add the console.log and no effect.

I put a break point on the console.log and no effect…

The function contents isn’t executed :O

Do you have an idea ?

 
Posted: 01 March 2011 09:23 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-17
278 posts

Does Firebug report any errors? Try replacing post() with more customizable ajax():

$.ajax({
    type
"POST",
    
url"create-account",
    
data"name=John&pass=test",
    
success: function(){
        console
.log('success')
    
},
    
error: function(){
        console
.log('error')
    
},
    
complete: function(){
        console
.log('completed')
    
}
}
); 
 
Posted: 01 March 2011 09:27 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2011-02-28
8 posts

error
completed

and the firebug msg is now :

“404 Page Not Found</h1>
The page you requested was not found.”

 
Posted: 01 March 2011 09:30 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-17
278 posts

Check your request headers and make sure URL is correct, change

url"create-account" 

accordingly.

 
Posted: 01 March 2011 09:34 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2011-02-28
8 posts

Ok, sorry it was an error.

Now the firebug response is : ‘data received’ (my php echo)

But, the same problem… my javascript doesnt work :(

 
Posted: 01 March 2011 09:36 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-17
278 posts

Can you elaborate? In what way it “doesn’t work”?

 
Posted: 01 March 2011 09:38 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2011-02-28
8 posts

My ajax method call the Php with good arguments and the Php is executed.
Here, all is ok.

But why the ‘success’ is’nt launched ? :(

 
Posted: 01 March 2011 09:47 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-17
278 posts

Is ‘error’ string being logged in console? If so, update

error: function(){
    console
.log('error')

with

error: function(jqXHRtextStatuserrorThrown){
    console
.log(jqXHR);
    
console.log(textStatus);
    
console.log(errorThrown);

and check Firebug console for returned variables. They should give you information why the call is failing.

 
Posted: 02 March 2011 12:51 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-06
77 posts

You should:

$.post("<?=base_url()?>create-account"{ name"John"pass"test" }, function(data){
     alert(
"Data Loaded: " data);
   
});