1. 程式人生 > >axios 簡單筆記

axios 簡單筆記

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js"></script>
</head>

<body>
    <div id="app"></div>
</body> <script> /** * get 請求 */ axios.get('http://192.168.31.97/index.php?a=123').then(response => { console.log(20181021221522, response) }) /** * post application/x-www-form-urlencoded;charset=utf-8 * https://github.com/axios/axios#browser * 推薦使用 qs: * $ cnpm install qs * const params = qs.stringify({ 'a': 123 })
*/ const params = new URLSearchParams(); params.append('a', '123'); axios.post('http://192.168.31.97/index.php', params, { headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'} }).then(response => { console.log(20181021221338, response) }) /** * post application/json;charset=utf-8
*/ axios.post('http://192.168.31.97/index.php', {a: 123}).then(response => { console.log(20181021221338, response) }) // ajax(預設是application/json;charset=utf-8) axios({ method: 'post', url: 'http://192.168.31.97/index.php', data: { firstName: 'Fred', lastName: 'Flintstone' } }).then(response => { console.log(20181021225057, response) }) // ajax(指定為application/x-www-form-urlencoded;charset=utf-8) const params2 = new URLSearchParams(); params2.append('firstName', 'Fred'); params2.append('lastName', 'Flintstone'); axios({ method: 'post', url: 'http://192.168.31.97/index.php', data: params2, headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'}, }).then(response => { console.log(20181021225057, response) }) </script> </html>

 

 

index.php

<?php 

header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Headers:x-requested-with,content-type'); 

// post(form) 和 get請求
var_dump($_REQUEST);
// application/json;charset=utf-8 的資料必須這樣使用
var_dump($GLOBALS['HTTP_RAW_POST_DATA']);