Welcome Guest, Not a member yet? Register   Sign In
Return data to Ajax call
#1

[eluser]mast3rpyr0[/eluser]
Hey guys, this one has been stumping me since my last post here.. I can't manage to receive any data from my ajax call to log a user in. It is successully creating the session, so the php does execute, but nothing is in the 'data' variable when it returns. It could just be my lack of jquery know how but I'm completely stumped, I feel like I have tried everything.

If instead of returning data I do a redirect, my data variable will have the code of the page it would have redirected to. Using returns, echos, or prints however with a normal string or json data, do not return any data.

Heres my code:

php
Code:
if($this->form_validation->run() == false)
  {
   return json_encode(array('message' => validation_errors()));
  }
  else
  {
   $username = $this->input->post('username');
   $password = $this->input->post('password');
  
   $this->load->model('account_model');
  
   if($this->account_model->validate_credentials($username, $password))
   {
    $sessiondata = array(
     'username' => $username,
     'loggedin' => true
    );
  
    $this->session->set_userdata($sessiondata);
    return json_encode(array('message' => 'success'));
   }
   else
   {
    return json_encode(array('message' => 'Invalid Username or Password'));
   }
  }

ajax call
Code:
$("#loginform").submit(function(event){
     event.preventDefault();
    
     $.post($(this).attr('action'), $(this).serialize(), function(data){
      alert("Data: " + data);
     });
    });
#2

[eluser]mast3rpyr0[/eluser]
Just a quick bump, I'm still completely stuck on this and can't get much else done til I figure this out ><
#3

[eluser]CroNiX[/eluser]
return only works with php. You need to echo out the json response from the controller. You might try telling your jQuery $.post() that json is the expected format for returned data. Also, what is firebug telling you about what is getting transmitted, where it's getting sent to and what data is being returned?

When you send the data to the alert function in your callback, try using $(data).message instead of just data.
#4

[eluser]mast3rpyr0[/eluser]
Im using chrome, so hopefully this data is similar to what firebug would give.. but this is what is returned:

Code:
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:47
Content-Type:text/html
Date:Sat, 18 Feb 2012 18:15:17 GMT
Keep-Alive:timeout=5, max=91
Server:Apache/2.2.17 (Win32) PHP/5.3.4
Set-Cookie:ci_session=a:7:{s:10:"session_id";s:32:"b938ba43aa1df977fe47592f4d74eff2";s:10:"ip_address";s:9:"127.0.0.1";s:10:"user_agent";s:108:"Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/535.19+(KHTML,+like+Gecko)+Chrome/18.0.1025.33+Safari/535.19";s:13:"last_activity";i:1329588914;s:9:"user_data";s:0:"";s:8:"username";s:10:"mast3rpyr0";s:8:"loggedin";b:1;}5e4c17368295579b8ba6161315de04fb; expires=Sat, 18-Feb-2012 20:15:17 GMT; path=/
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.4

Could the gzip be effecting it? I tried shutting it off but even doing so and clearing cookies still sent me gziped data back.

This is running on a local dev server, could that be an issue too?

Replaced returns with echos, my alert is working again but I get "Data: undefined" still.


EDIT: Removing the ajax post, im getting an error on the page which must be the reason for this issue.

Error 330 (net::ERR_CONTENT_DECODING_FAILED): Unknown error.

So this looks related to the gzip possibly?



EDIT 2: Disabling gzip in both config.php and .htaccess fixes the issue. Now, how could i get this to work while using gzip on bigger pages at least?
#5

[eluser]Unknown[/eluser]
value should be return in html not in json on ajax call
#6

[eluser]MrBaseball34[/eluser]
Set your data_type to 'application/json' and it'll work for you.




Theme © iAndrew 2016 - Forum software by © MyBB