1. 程式人生 > 實用技巧 >Vue 端解決 axios post 表單,servlet使用 request.getParameter() 接受為 null 的問題

Vue 端解決 axios post 表單,servlet使用 request.getParameter() 接受為 null 的問題

參考地址:https://segmentfault.com/q/1010000008462977

      https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options

1.

2.使用 Node.js帶的querystring.stringify

首先在main.js裡面import進來

  import querystring from 'querystring'

  Vue.prototype.$querysting;

然後在所需元件內部

  let data = {
    username: this.loginForm.username,
    password: this.loginForm.password,
    captcha:this.loginForm.captcha
  }

  this.$axios.post(url,this.$qs.stringify(data))  .then((resp)=>{alert(resp.data)}

關於這個stringify用法Node.js官網解釋:

querystring.stringify(obj[, sep[, eq[, options]]])#

Added in: v0.1.25
  • obj<Object>The object to serialize into a URL query string
  • sep<string>The substring used to delimit key and value pairs in the query string.Default:'&'
    .
  • eq<string>. The substring used to delimit keys and values in the query string.Default:'='.
  • options
    • encodeURIComponent<Function>The function to use when converting URL-unsafe characters to percent-encoding in the query string.Default:querystring.escape().

Thequerystring.stringify()

method produces a URL query string from a givenobjby iterating through the object's "own properties".

It serializes the following types of values passed inobj:<string>|<number>|<boolean>|<string[]>|<number[]>|<boolean[]>Any other input values will be coerced to empty strings.

querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
// Returns 'foo=bar&baz=qux&baz=quux&corge='

querystring.stringify({ foo: 'bar', baz: 'qux' }, ';', ':');
// Returns 'foo:bar;baz:qux'

By default, characters requiring percent-encoding within the query string will be encoded as UTF-8. If an alternative encoding is required, then an alternativeencodeURIComponentoption will need to be specified:

// Assuming gbkEncodeURIComponent function already exists,

querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
                      { encodeURIComponent: gbkEncodeURIComponent });