dongxingchang9345
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.

图片转代码服务由CSDN问答提供 功能建议

我正在使用Aurelia和PHP作为后端。 以下是我的模型视图:

home.html

 &lt; template&gt; 
&lt;  ; require from =“datepicker.js”&gt;&lt; / require&gt; 
&lt; form submit.delegate =“submit()”&gt; 
&lt; input value.bind =“name”&gt;&lt; / input&gt  ; 
&lt; input value.bind =“age”&gt;&lt; / input&gt; 
&lt; button type =“submit”name =“submit”&gt;提交
&lt; / button&gt; 
&lt; / 形式&GT; 
&LT; /模板&GT; 
   
 
 

<强> home.js

 <代码>进口 来自'aurelia-framework'的{inject};来自'aurelia-fetch-client'的
import {HttpClient}; 
import'fetch'; 
 
 @ inject(HttpClient)
export class Home {
 name; 
  age; 
 
构造函数(http){
 this.http = http; 
} 
 
 submit(){
 this.jsonobj = {
'name':this.name,
'  age':this.age 
}; 
 
 if(this.jsonobj.name&amp;&amp; this.jsonobj.age){
 this.http.fetch('dist / components / ses.php',  {
方法:'post',
 body:JSON.stringify(this.jsonobj)\  n})
 .then(response =&gt;  response.json())
 .then(data =&gt; 
 {
 console.log(data); 
}); 
} 
} 
} 
   
 
 

以下是PHP脚本:

ses.php

   &lt;?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'=&gt; $  _SESSION ['username'])); 
 
?&gt; 
   
 
 

我希望在第一次调用脚本后,$ _SESSION ['username' ]将被设置为'kil'。 所以在下一个ajax帖子!isset($ _ SESSION ['username']不会评估为true但它确实意味着PHP会话无效。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushai4890
    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)
      })
    
    点赞 评论

相关推荐