Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active June 24, 2017 06:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgqfrms-GitHub/a3fcffb6d7923e2a7471041053f00162 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/a3fcffb6d7923e2a7471041053f00162 to your computer and use it in GitHub Desktop.
HTTPS/HTTP Ajax Header & CORS & mlab, mongodb, db, json, api, free , online,

HTTPS/HTTP Ajax Header & CORS

mlab, mongodb, db, json, api, free , online,

https://gist.github.com/xgqfrms-GitHub/636260e8332f0fe4f530fe040ffd3741

    
fetch('https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest',{
    method: 'get',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'X-Api-Key': `0987b00e542141369049e647701b65d9`
    }
})
.then(function(response) {
    return response.json()
}).then(function(json) {
    console.log('parsed json: ', json)
}).catch(function(error) {
    console.log('parsing failed: ', error)
});

在正文中指定空列表等同于删除与查询匹配的文档。

    

// {"id": 2017003, "name": "test", "age": 27}

$.ajax({
    url: "https://api.mlab.com/api/1/databases/node-mongodb/collections/test?apiKey=pa4Yku0O7y6CHLqKwDGlLKSfkdPfQprR",
    data: JSON.stringify([]),
    type: "PUT",
    contentType: "application/json" 
});

$.ajax({
    url: "https://api.mlab.com/api/1/databases/node-mongodb/collections/test?apiKey=pa4Yku0O7y6CHLqKwDGlLKSfkdPfQprR",
    type: "DELETE",
    accepts: {
        contentType: "application/json",
    },
    async: true,
    timeout: 300000,
    success: function (data) {
        console.log(data);
     },
    error: function (xhr, status, err) {
        console.log(xhr, status, err);
    }
});

Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response.

http://docs.mlab.com/data-api/#delete-documents

Access-Control-Allow-Methods

response headers

headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");

https://stackoverflow.com/questions/36374247/always-got-method-delete-is-not-allowed-by-access-control-allow-methods-in-prefl

https://stackoverflow.com/questions/28990565/delete-is-not-allowed-by-access-control-allow-methods

https://stackoverflow.com/questions/39312736/method-delete-is-not-allowed-by-access-control-allow-methods-in-preflight-respon

https://api.jquery.com/jQuery.ajax/

    
$.ajax({
  accepts: {
    mycustomtype: 'application/x-some-custom-type'
  },
  // Instructions for how to deserialize a `mycustomtype`
  converters: {
    'text mycustomtype': function(result) {
      // Do Stuff
      return newresult;
    }
  },
  // Expect a `mycustomtype` back from server
  dataType: 'mycustomtype'
});

http://docs.mlab.com/data-api/#delete-documents

delete & update

    
$.ajax({
    url: 'https://api.mlab.com/api/1/databases/my-db/collections/my-coll?apiKey=myAPIKey',
    data: JSON.stringify([{"x": 1}, {"x": 2}, {"x": 3}]),
    type: "PUT",
    contentType: "application/json" 
});

??? DELETE

Specifying an empty list in the body is equivalent to deleting the documents matching the query.

在正文中指定空列表等同于删除与查询匹配的文档。

    
$.ajax({
    url: 'https://api.mlab.com/api/1/databases/my-db/collections/my-coll?apiKey=myAPIKey',
    data: JSON.stringify([{"x": 1}, {"x": 2}, {"x": 3}]),
    type: "DELETE",
    contentType: "application/json" 
});
@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jun 24, 2017

no-cors

https://gist.github.com/xgqfrms-GitHub/68af127cb3ec33587ee5bffe9810fb1b

站点流量统计

https://gist.github.com/xgqfrms-GitHub/6a16df85644b814265abe98150280d0f

let myImage = document.querySelector('img');

let myHeaders = new Headers();
myHeaders.append('Content-Type', 'image/png');

const myInit = {
    method: 'GET',
    headers: myHeaders,
    mode: 'cors',
    cache: 'default'
};

let myRequest = new Request('https://cdn.xgqfrms.xyz/logo/icon.png');

fetch(myRequest, myInit)
.then(function(response) {
    return response.blob();
})
.then(function(blob) {
    var objectURL = URL.createObjectURL(blob);
    myImage.src = objectURL;
});

@xgqfrms-GitHub
Copy link
Author

Revealing-Prototype-Pattern & IIFE

https://stackoverflow.com/a/40930502/5934465

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment