zoukankan      html  css  js  c++  java
  • 日常看贴的零碎点

    restFul风格:
    非REST的url:http://…../queryItems.action?id=001&type=T01
    REST风格的url:http://…./id/001/type/T01

    ajax的写法:

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState === 4 && this.status === 200) {
    console.log(this.responseText);
    }
    };
    xhttp.open("GET", "/", true);
    xhr.onload = function() {
    console.log(xhr.response);
    };

    xhr.onerror = function() {
    console.log("Oops, error");
    };
    xhttp.send();



    fetch:
    是浏览器提供的原生ajax接口
    fetch的用法:

    // url (必须), options (可选)
    fetch('/some/url', {
    method: 'get'
    }).then(function(response) {

    }).catch(function(err) {
    // 出错了;等价于 then 的第二个参数,但这样更好用更直观 :(
    });

    // 链式处理,将异步变为类似单线程的写法: 高级用法.
    fetch('/some/url').then(function(response) {
    return //... 执行成功, 第1步...
    }).then(function(returnedValue) {
    // ... 执行成功, 第2步...
    }).catch(function(err) {
    // 中途任何地方出错...在此处理 :(
    });

    //对请求响应头的操作

    // 创建一个空的 Headers 对象,注意是Headers,不是Header
    var headers = new Headers();

    // 添加(append)请求头信息
    headers.append('Content-Type', 'text/plain');
    headers.append('X-My-Custom-Header', 'CustomValue');

    // 判断(has), 获取(get), 以及修改(set)请求头的值
    headers.has('Content-Type'); // true
    headers.get('Content-Type'); // "text/plain"
    headers.set('Content-Type', 'application/json');

    // 删除某条请求头信息(a header)
    headers.delete('X-My-Custom-Header');

    // 创建对象时设置初始化信息
    var headers = new Headers({
    'Content-Type': 'text/plain',
    'X-My-Custom-Header': 'CustomValue'
    });

    var request = new Request('/some-url', { headers: new Headers({ 'Content-Type': 'text/plain' }) }); fetch(request).then(function() { /* handle response */ });

    var uploadReq = new Request("/uploadImage", { method: "POST", headers: { "Content-Type": "image/png", }, body: "image data" });

    fetch(uploadReq ).then(function() { /* handle response */ });

  • 相关阅读:
    Typescript+WebGL+Webpack开发环境搭建
    SVG的动态之美-搜狗地铁图重构散记
    2017年个人总结-程序员的中年焦虑症
    上海2017QCon个人分享总结
    CSS预编译与PostCSS以及Webpack构建CSS综合方案
    前端工程师的基本素养
    不仅仅是复制粘贴
    《微信小程序七日谈》- 第七天:不要捡了芝麻丢了西瓜
    《微信小程序七日谈》- 第六天:小程序devtool隐藏的秘密
    《微信小程序七日谈》- 第五天:你可能要在登录功能上花费大力气
  • 原文地址:https://www.cnblogs.com/cxdxm/p/9205803.html
Copyright © 2011-2022 走看看