dongxingchang9345 2016-04-15 11:27
浏览 45
已采纳

PHP会话无法在Aurelia中运行

I'm using Aurelia with PHP as backend. Here is my view with its model:

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);
          });
    }
  }
}

And here is the PHP script:

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']));

?>

I expect that after the first call to the script, $_SESSION['username'] will be set to 'kil'. So on next ajax post !isset($_SESSION['username'] wont evaluate to true but it does which means PHP session isn't working.

  • 写回答

1条回答 默认 最新

  • doushai4890 2016-04-15 12:18
    关注

    By default fetch (the web standard that aurelia-fetch-client is built on) does not send cookies. You'll need to use credentials: 'include' in your request init object.

    Two great resources for fetch:

    The codez:

    this.http.fetch('dist/components/ses.php', {
        credentials: 'include', // <-------------------------------------
        method: 'post',
        body: JSON.stringify(this.jsonobj)
      })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 第二个问题该怎么解决啊,完全不会编写,不会做库文件,也不知道怎么写这个代码
    • ¥15 怎么使请求通过cors
    • ¥15 WDM 驱动ACPI 相关疑问
    • ¥15 prism 跨窗体共享数据绑定 wpf
    • ¥15 hdl designer突然用不了系统的moduleware组件,请问有人遇到或者怎么解决吗?
    • ¥15 0基础计算机毕设,应该从哪开始?
    • ¥60 使用DKT40脑图划分ROI区域
    • ¥15 有偿解决C51单片机液晶屏12864显示问题
    • ¥15 IDEA构建失败?怎么搞
    • ¥15 求该题的simpson,牛顿科特斯matlab代码,越快越好