【问题标题】:PHP session not working in AureliaPHP 会话在 Aurelia 中不起作用
【发布时间】:2016-04-15 11:27:42
【问题描述】:

我使用 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'] 不会评估为真,但它确实意味着 PHP 会话不工作。

【问题讨论】:

    标签: javascript php ajax aurelia


    【解决方案1】:

    默认情况下fetch(aurelia-fetch-client 所基于的网络标准)不发送 cookie。你需要在你的请求初始化对象中使用credentials: 'include'

    两个很好的获取资源:

    密码:

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

    【讨论】:

    • 哥们,你拯救了我的一天!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-06-28
    • 2017-02-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多