zoukankan      html  css  js  c++  java
  • 一个和与后台数据连接的模板get post put 以及延伸的query

    /*
    example:

    require.config({
    paths: {
    "httpClient": "../../core/http-client"
    }
    });

    require(['httpClient'], function (httpClient) {
    httpClient.get("categories", function (res) {
    console.log(res)
    })
    })
    */

    define(["jquery"], function ($) {

    // const API_URL = "http://wcapi.novonity.com/api/v1/";
    const API_URL = "http://192.168.2.63:8989/api/v1/";

    var get = function (url, success, fail) {
    request({
    type: 'get', url: API_URL + url, success: function (res) {
    hideDataLoading();
    success && success(res);
    }, error: fail
    })
    };

    var query = function (url, queryParams, success, fail) {
    request({
    type: 'get', url: API_URL + url + '?' + $.param(queryParams), success: function (res) {
    hideDataLoading();
    success && success(res);
    }, error: fail
    })
    };

    var put = function (url, data, success, fail) {
    request({
    type: 'put', data: data, url: API_URL + url, success: function (res) {
    hideDataLoading();
    success && success(res);
    }, fail: fail
    })
    };

    var post = function (url, data, success, fail) {
    request({
    type: 'post', data: data, url: API_URL + url, success: function (res) {
    hideDataLoading();
    success && success(res);
    }, fail: fail
    })
    };

    return {
    get: get,
    query: query,
    put: put,
    post: post
    };

    function request(options) {
    showDataLoading();
    $.ajax(options);
    }

    function showDataLoading() {
    if (document.getElementById('dataLoading')) {
    return;
    }
    var div = document.createElement("div");
    div.setAttribute("id", "dataLoading");
    div.setAttribute("style", getRefreshDivStyle());
    div.innerHTML = "加载中...";
    document.body.appendChild(div);
    }

    function hideDataLoading() {
    $("#dataLoading").remove();
    }

    function getRefreshDivStyle() {
    return " 160px;height: 100px;text-align: center;color: #fff;position: fixed;top: 50%;left: 50%;margin-left: -80px;" +
    "margin-top: -50px;background-color: rgba(0, 0, 0, 0.6);z-index: 9999;line-height: 100px;border-radius: 5px;font-size: 16px;";
    }
    });

  • 相关阅读:
    [Redis-CentOS7]Redis设置连接密码(九)
    [Redis-CentOS7]Redis数据持久化(八)
    PAT Advanced 1101 Quick Sort (25分)
    PAT Advanced 1043 Is It a Binary Search Tree (25分)
    HTTP协议
    PAT Advanced 1031 Hello World for U (20分)
    自然码双拼快速记忆方案
    macOS 原生输入法设置自然码
    PAT Advanced 1086 Tree Traversals Again (25分)
    PAT Advanced 1050 String Subtraction (20分)
  • 原文地址:https://www.cnblogs.com/wangxiaoer5200/p/8695004.html
Copyright © 2011-2022 走看看