2016-04-15 6 views
0

私はAureliaをバックエンドとしてPHPを使用しています。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 post!isset($ _ SESSION ['username']はtrueに評価されませんが、PHPセッションが機能していないことを意味します)

答えて

4

デフォルトではfetch(aurelia-fetch-clientが構築するWeb標準。クッキーを送信しない)にあなたはあなたの要求のinitオブジェクトにcredentials: 'include'を使用する必要があります

二つの偉大なリソースを取得するために:。

codez:

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

Thaks。それはうまくいった。 –

+0

あなたは私の一日を救った!どうも! –