L
L
last_round2017-07-25 10:02:47
PHP
last_round, 2017-07-25 10:02:47

How to send a post request with VueJS?

Good afternoon, help who than can.
I have a registration form:

<form v-on:submit.prevent="register()" class="form-register">
    <div class="login">
      <div class="login__text">Логин</div>
      <input v-model="dataform.login" type="text" class="login__input">
    </div>
    <div class="password">
      <div class="password__text">Пароль:</div>
      <input v-model="dataform.password" type="password" class="password__input">
    </div>
    <div class="email">
      <div class="email__text">E-mail:</div>
      <input v-model="dataform.email" type="e-mail" class="email__input">
    </div>
    <button class="register"><span>Зарегистрироваться</span></button>
  </form>

The register() function sends a post request as a php file:
vm = new Vue
  el: "#app"
  data:
    dataform: {}
  methods:
    register: ->
      console.log @dataform
      @$http.post("includes/register.php?login=#{@dataform.login}&pass=#{@dataform.password}&email=#{@dataform.email}").then((res) -> 
        console.log res
      (error) -> 
        alert 'Error')

php file:
header('Content-type: application/json');
  echo json_encode($_POST);

The request is sent and the response status is "OK - 200".
The problem itself, I can’t figure out how to return the sent data from php and how to check that php accepted them at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
last_round, 2017-07-25
@last_round

All figured out.
vuejs:

@$http.post("includes/register.php?dataform=", @dataform).then((res) -> 
        console.log res
      (error) -> 
        alert 'Error')

php:
$json = file_get_contents('php://input'); 
  $data = json_decode($json, JSON_BIGINT_AS_STRING);
  print_r($data['password']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question