zoukankan      html  css  js  c++  java
  • axios post提交的Content-Type

    使用axios的坑

    jQuery.ajaxpost提交默认的请求头的Content-Type: application/x-www-form-urlencoded
    axios.post提交的请求头是Content-Type: application/json

    application/json是一个趋势,但是如果改一个旧项目,把jQuery.ajax全部换成axios.post时,需要对请求做一些配置。

    改之前的代码:

    // 没有指定请求头的content-type
    var data = {age: 18};
    $.ajax({
        url: '',
        type: 'POST',
        data: data
        dataType: 'json',
        success: function(result) {
            // do something
        }
    })

    使用axios的代码

    import axios from 'axios';
    import qs from 'qs';
    
    var data = {age: 18};
    var url = '';
    
    axios.post(
        url, 
        qs.stringify(data), 
        {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
    ).then(result => {
        // do something
    })

    https://www.cnblogs.com/changzhenan/p/8430760.html

  • 相关阅读:
    刷题238. Product of Array Except Self
    刷题236. Lowest Common Ancestor of a Binary Tree
    刷题208. Implement Trie (Prefix Tree)
    A1070
    A1048
    A1050
    A1041
    A1092
    A1084
    n进制转十进制
  • 原文地址:https://www.cnblogs.com/niyl/p/14736464.html
Copyright © 2011-2022 走看看