S
S
shane892015-06-30 07:59:54
JavaScript
shane89, 2015-06-30 07:59:54

Not sending data via ajax to kohana?

Hello. I'm doing a small service, it works on kohane, ajax.
Doesn't want to write data via ajax, 500 (Internal Server Error) I don't understand what the error is.
Here is the controller code that receives the data

$data = array();
    $auth = Auth::instance();
    $phone = Arr::get($_POST, 'phone', '');
    $password = Arr::get($_POST, 'password', '');
    $name = Arr::get($_POST, 'username', '');
    $email = Arr::get($_POST, 'email', '');

    $passwordHash = $auth->hash_password($password);
 
    $registration = new Model_Register();

    $res = $registration->reg($email, $name, $phone, $passwordHash);
    echo json_encode($data['result'] => $res));

Here is the model to process
public function reg($email, $name, $phone, $passwordHash)
  {
    $user = new Model_User();

    $user->email = $email;
    $user->username = $name;
    $user->phone = $phone;
    $user->password = $passwordHash;
    $user->save();

    return true;
  }

and ajax itself
$.ajax({
        type:'POST',
        url:'/reg/',
        dataType:'json',
        data:{
          phone:phone,
          password:pass,
          username:name,
          email:email
        },
        success:function(data){
          if(data == 'ok'){
            alert('все ок');
          }else{
            alert('ошибка');
          }
        },
        error:function(data){
          alert(data);
        }
      });

What is wrong here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan GiBSON, 2015-07-25
@gibson_dev

Why at all?
And in the second, when adding a user, there is no need to hash the password, the rules are set in the model - when you specify the field with the password, it will hash itself.
Therefore enough

$user = new Model_User();
    $user->email = $email;
    $user->username = $name;
    $user->phone = $phone;
    $user->password = $password;
    $user->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question