0

I'm trying to make an AJAX call in Vue to a PHP script, but it doesn't seem to be working.

Vue:

methods: {
  onSubmit () {
    if (this.valid) {
      this.$http.post('http://remindwordserver.loc/register.php', {test: 'test'}).then(response => {
        console.log(response.body)
      }, response => {})
    }
  }
},

PHP:

`<?php

print_r($_POST);` 

$_POST is empty

What am I doing wrong?

Venantius
  • 2,400
  • 2
  • 26
  • 35

2 Answers2

0

I'm not too familiar with PHP, but I'll give it a go.

The $_POST variable only contains data from the request when the HTML Content-Type of the request is application/x-www-form-urlencoded or multipart/form-data. vue-resource by default sets the Content-Type of the request to application/json.

If you want to access JSON data in your PHP script, you'll have to decode the request data from JSON. See Receive JSON POST with PHP.

Decade Moon
  • 30,718
  • 8
  • 70
  • 91
0
  • Use file_get_contents('php://input') instead of $_POST
  • Also you must Configure appche server to response external request create .htaccess file and add this content:

    
      Header always set Access-Control-Allow-Origin "http://localhost:8080"
      Header always set Access-Control-Max-Age "1000"
      Header always set Access-Control-Allow-Headers "X-Requested-With, Content-    Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding"
      Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
      RewriteEngine On
    RewriteCond %{REQUEST_METHOD} OPTIONS RewriteRule ^(.*)$ phpFile.php [QSA,L]
Niyaz
  • 159
  • 8