小编Fer*_*sso的帖子

ajax,setRequestHeader(),Content-Type,application/x-www-form-urlencoded和charset

当内容类型不是text/html,text/plain或text/xml时,我无法理解如何设置charset,而是app/x-www-form-urlencoded内容类型.

给出这个(简化的)javascript代码:

var xhr = new XMLHttpRequest();
Run Code Online (Sandbox Code Playgroud)

如果我没有明确设置编码,

xhr.open('POST', 'serv.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
Run Code Online (Sandbox Code Playgroud)

萤火告诉我,内容类型是"应用程序/ X WWW的窗体-urlencoded;字符集= UTF-8 ".

例如,如果我将charset设置为ISO-8859-1,

xhr.open('POST', 'serv.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
Run Code Online (Sandbox Code Playgroud)

firebug 仍然告诉我"application/x-www-form-urlencoded; charset = UTF-8."

如果我尝试类似的东西

xhr.setRequestHeader('Content-Type', 'text/plain; charset=ISO-8859-1');
Run Code Online (Sandbox Code Playgroud)

然后它尊重charset.

在所有情况下,send()方法如下所示:

xhr.send('id=9&name=Yoda');
Run Code Online (Sandbox Code Playgroud)

如果Content-Type是x-www-form-urlencoded,为什么不尊重我指定的charset?

注意:我正在使用ISO-8859-1作为示例.我的目标是了解正在发生的事情.

javascript ajax character-encoding

10
推荐指数
1
解决办法
5万
查看次数

如何使用Vue Test Utils测试全局事件总线?

我正在尝试学习如何测试通过全局事件总线发出的事件.这是代码,在我不知道该怎么做的地方有一些评论.

// EvtBus.js
import Vue from 'vue';
export const EvtBus = new Vue();
Run Code Online (Sandbox Code Playgroud)
<!-- CouponCode.vue -->
<template>
    <div>
        <input
            class="coupon-code"
            type="text"
            v-model="code"
            @input="validate">
        <p v-if="valid">
            Coupon Redeemed: {{ message }}
        </p>
    </div>
</template>

<script>

import { EvtBus } from '../EvtBus.js';

export default {
    data () {
        return {
            code: '',
            valid: false,

            coupons: [
                {
                    code: '50OFF',
                    discount: 50,
                    message: '50% Off!'
                },
                {
                    code: 'FREE',
                    discount: 100,
                    message: 'Entirely Free!'
                }
            ]
        };
    },

    created () {
        EvtBus.$on('coupon-applied', () …
Run Code Online (Sandbox Code Playgroud)

unit-testing vue.js vue-test-utils

8
推荐指数
2
解决办法
2714
查看次数

不能使用$('.foo').on('click','.bar',使用jQuery和TypeScript

我在项目中使用TypeScript和jQuery,我需要使用jQuery on来监听事件:

const $previewListWrp: jQuery<HTMLElement> = $('.js-preview-list-wrp').first();

$previewListWrp.on('click', '.btn-action.remove', (evt: Event): void => {
  let $box: JQuery<HTMLElement> = $(evt.target).closest('.js-preview-wrp');
  console.log($box.data());
});
Run Code Online (Sandbox Code Playgroud)

但是,我得到了错误

"click"类型的参数不能分配给'PlainObject void> |类型的参数 EventHandlerBase

刚刚开始一个新项目,并安装了最新版本的TypeScript,jQuery和@ types/jQuery.

在此输入图像描述

jquery events typescript

4
推荐指数
1
解决办法
1259
查看次数

nodejs + postgresql方式太慢

我有这段代码:

var pg = require('pg');
var QueryStream = require('pg-query-stream');
var constr = 'postgres://devel:1234@127.0.0.1/tcc';
var JSONStream = require('JSONStream');
var http = require('http');

pg.connect(constr, function(err, client, done) {
    if (err) {
        console.log('Erro ao conectar cliente.', err);
        process.exit(1);
    }

    sql = 'SELECT \
          pessoa.cod, \
          pessoa.nome, \
          pessoa.nasc, \
          cidade.nome AS cidade \
          FROM pessoa, cidade \
          WHERE cidade.cod IN (1, 2, 3);';

    http.createServer(function (req, resp) {
        resp.writeHead(200, { 'Content-Type': 'text/html; Charset=UTF-8' });
        var query = new QueryStream(sql);
        var stream = client.query(query);

        //stream.on('data', …
Run Code Online (Sandbox Code Playgroud)

postgresql node.js

3
推荐指数
1
解决办法
3396
查看次数