2016-04-15 117 views
0

我使用PHP作为后端的Aurelia。这是我与它的模型视图:PHP会话不在Aurelia工作

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 ['用户名']不会评估为true,但它确实意味着PHP会话不工作

回答

4

默认fetch(构建aurelia-fetch-client的web标准。上)不发送cookie,您需要在您的要求初始化对象使用credentials: 'include'

两个伟大的资源获取:

的codez:

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

Thaks。这工作。 –

+0

哥们你救了我的一天!谢谢! –