-1

I just want to get back some return value from the ajax data post. I am not sure why I am not getting something back in success. Please review the code and tell me where I am wrong

My jquery code

$("#btnlogin").click(function(){

    var email = $("#emaillog").val();
    var password = $("#passlog").val();
    console.log('test');
    /* $.ajax({
        url: 'home2/login_user',
        //data: {'title': title},  change this to send js object
        type: "post", 
        data: 'email=' + email+'&password='+password,
        success: function(output) {
            url:"home2/login_user",
            data: 'email=' + email+'&password='+password,
            alert(output);
        }
    });*/

    $.ajax ({
         url:"home2/login_user",
         type: "post",
         dataType: 'json', 
         data: 'email=' + email+'&password='+password,
         success: function (data) {
             $.each(data, function(key, value) {
                 console.log(key,'--',value);
             });
              //iterate here the object
         }
     });
}); 

My php code

public function Login_user()
    {
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $data['result'] = $this->Home_model->login_to_user($email,$password); 
        echo json_encode ($data['result']);     


    }

In php code I echo the result but in in jquery. I am not getting any result in success

Thanks

rrk
  • 15,677
  • 4
  • 29
  • 45
Azad Chouhan
  • 47
  • 1
  • 11

3 Answers3

1

use parseJSON

var obj = jQuery.parseJSON(data);
console.log(obj.key);
Parth Patel
  • 521
  • 2
  • 12
1

The reason is because your backend code does not seem to be able to find the username and password parameters. You're passing them the wrong way at this line of code:

data: 'email=' + email+'&password='+password,

Replace the string with a JavaScript object:

data: { email: email, password: password }

dimlucas
  • 5,040
  • 7
  • 37
  • 54
0

well, first, you are passing the parameters like it was a get request, and its not.

Change the way you passing to something like this.

 $.ajax ({
         url:"home2/login_user",
         type: "post",
         dataType: 'json', 
         data: { field1: "hello", field2 : "hello2"},
         success: function (data) {
             $.each(data, function(key, value) {
                 console.log(key,'--',value);
             });
              //iterate here the object
         }
     });
PlayMa256
  • 6,603
  • 2
  • 34
  • 54