簡體   English   中英

PHP會話在Aurelia中不起作用

[英]PHP session not working in Aurelia

我將Aurelia與PHP一起用作后端。 這是我對其模型的看法:

home.html的

<template>
    <require from="datepicker.js"></require>
    <form submit.delegate="submit()">
        <input value.bind="name"></input>
        <input value.bind="age"></input>
        <button type="submit" name="submit">Submit
        </button>
    </form>
</template>

home.js

import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';

@inject(HttpClient)
export class Home {
  name;
  age;

  constructor(http) {
    this.http = http;
  }

  submit() {
    this.jsonobj = {
      'name': this.name,
      'age': this.age
    };

    if (this.jsonobj.name && this.jsonobj.age) {
      this.http.fetch('dist/components/ses.php', {
        method: 'post',
        body: JSON.stringify(this.jsonobj)
      })
        .then(response =>  response.json())
          .then(data => 
          { 
            console.log(data);
          });
    }
  }
}

這是PHP腳本:

ses.php

<?php
    session_start();

    $word = 'lol';
    if(!isset($_SESSION['username'])){
        $_SESSION['username'] = 'kil';
    }

    $input = file_get_contents('php://input');
    $input_json_array = json_decode($input);

    echo json_encode(array('item' => $_SESSION['username']));

?>

我希望在第一次調用腳本之后,$ _ SESSION ['username']將被設置為'kil'。 因此,在下一個ajax帖子中!isset($ _ SESSION ['username']不會評估為true,但這確實意味着PHP會話無法正常工作。

默認情況下, fetch (建立aurelia-fetch-client的Web標准)不會發送cookie。 您需要在請求init對象中使用credentials: 'include'

可獲取的兩個重要資源:

編碼:

this.http.fetch('dist/components/ses.php', {
    credentials: 'include', // <-------------------------------------
    method: 'post',
    body: JSON.stringify(this.jsonobj)
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM