Vue.js Axios发布发送空值

时间:2019-03-06 21:52:41

标签: javascript php vue.js

我在使用axios时遇到问题。

Vue文件:

<script>
export default {
  name: 'npcs-list',
  data () {
    return {
      data: '',
      index: '',
      dialogues: '',
      input: {
        npc_name: '',
      },
      npc_id: '',
      npc_name: '',
      npc_start_dialogues:'',
      dialogues: [],
      fields: [{
        name: 'id',
        callback: 'stringToInt'
      },
      {
        name: 'name'
      },
      {
        name: 'start_dialog'
      },
      {
        name: 'actions', // <----
        title: 'Akcje',
        titleClass: 'center aligned',
        dataClass: 'center aligned'

      }
      ],
      css: {
        pagination: {
          wrapperClass: 'pagination pull-right',
          activeClass: 'btn-primary',
          disabledClass: 'disabled',
          pageClass: 'btn btn-border',
          linkClass: 'btn btn-border',
          icons: {
            first: '',
            prev: '',
            next: '',
            last: ''

          }

        }
      }
    }
  },
  components: {
    Vuetable,
    VuetablePagination,
    VuetablePaginationInfo
  },
  methods: {
    insertNPC () {
      const data = this
      if (this.input.npc_name != '' && this.npc_start_dialogues != '') {
        axios.post('http://localhost/test/Quests/endpoints/insert.npc.php', {
          npc_name: 'dupa',
          npc_startdialog: 'dupa2'
        })
          .then((response) => {
            data.status = response.data
          })
          .catch(function (error) {
            console.log(error)
          })
        if (data.status == '1') {
          alert('Działa!')
        } else {
          alert('Taki tag już istnieje!')
        }
      } else {
        alert('Brak wystarczającej ilości danych')
      }
    },
    showModal(data) {
      this.$refs.myModalRef.show()
      this.data = data,
      this.npc_id = data.id,
      this.npc_name = data.name,
      this.npc_start_dialogues = data.start_dialog.split(";")
    },
    hideModal () {
      this.$refs.myModalRef.hide()
    },
    onHidden (evt){
      this.npc_id = '',
      this.npc_name = '',
      this.npc_start_dialogues = ''
    },
    log (data) {
      console.log(data)
    },
    editDialogue (data) {
      this.$router.push({
        name: 'editDialogues',
        params: {
          id: data
        }
      })
    },
    editOption (data) {
      this.$router.push({
        name: 'editOptions',
        params: {
          id: data
        }
      })
    },
}
</script>

PHP端点:

<?php

header('Content-Type:  text/html');

include_once '../Checkers/CheckIfNPCExists.php';
include_once '../Inserts/InsertNPC.php';

$requestBody = file_get_contents('php://input');
$data = json_decode($requestBody);
$status = new CheckIfNPCExists();

var_dump($data);

$result = $status->uniqueTest($data->npc_name);
if($result == '0'){
  $npc_class = new InsertNPC();
  $npc_result = $npc_class->createNew($data->npc_name,$data->npc_startdialog);
  echo $npc_result;
}

$ result给我一个有关获取'npc_name'的非对象属性的错误。

$ data的var_dump显示在Chrome预览中:

object(stdClass)[1]
  public 'npc_name' 

但是在响应中要多一些(xdebug日志):

<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\xampp\htdocs\test\Quests\endpoints\insert.npc.php:12:</small>
<b>object</b>(<i>stdClass</i>)[<i>1</i>]
  <i>public</i> 'npc_name' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'dupa'</font> <i>(length=4)</i>
  <i>public</i> 'npc_startdialog' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'dupa2'</font> <i>(length=5)</i>
</pre>

我将不胜感激,欢迎提供任何提示。文本限制太麻烦了。

0 个答案:

没有答案