gpt4 book ai didi

javascript - 使用 ajax 编辑要点

转载 作者:行者123 更新时间:2023-12-03 01:07:05 27 4
gpt4 key购买 nike

如何使用 ajax 更新要点?我试过这个:

$.support.cors = true;
$.ajax({
crossDomain: true,
//method: 'PATCH',
//method: 'POST',
//type: 'PATCH',
url: 'https://api.github.com/gists/<GIST_ID>',
data: '{"description" : "some description text","public" : true, "files" : { "myfile.txt" : { "content" : "file edited via ajax", "filename" : "myfile.txt" } }}',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization","Basic <USER>:<PASS>");
}
}).done(function(response) {
console.log(response);
});

documentation没有提供明确的例子。

它返回 JSON 和状态 OK 200,但要点未更新。

如果我使用方法POST,它将给出状态 404。

最佳答案

我编写了这个处理这一切的 JavaScript 对象:

/**
* Credit: http://techslides.com/github-gist-api-with-curl-and-ajax
* https://developer.github.com/v3/gists/

* @example
* var g = new Gist();
* g.gistID = 'ae7f4db52bbf76051e25e26fb05712c0';
* g.token = '2590a460f653d23c4f61c6774b7a41296aea9028';
*
* g.create({
* description: 'new gist',
* public: true,
* files: {
* 'file1.txt': {
* 'content': 'file contents'
* }
* }
* }, function(data) {
* // callback
* });
*/

var Gist = function() {

$.support.cors = true; // Whithout this ajax will not work in IE 9.
$.ajaxSetup({ cache: false }); // Disable ajax cache globally.

this.gistID = '';
this.token = '';

var self = this;

this.newToken = function(user, pass, callback) {
$.ajax({
url: 'https://api.github.com/authorizations',
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user + ':' + pass));
},
data: JSON.stringify({
scopes: ['gist'],
note: 'new token request from ajax ' + new Date()
})
}).always(function(data, txtStatus, err) {
if (typeof callback === 'function') callback(data);
});
}

this.create = function(data, callback) {
$.ajax({
url: 'https://api.github.com/gists',
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'token ' + self.token);
},
data: JSON.stringify(data)
}).always(function(data, txtStatus, err) {
if (typeof callback === 'function') callback(data);
});
}

this.edit = function(data, callback) {
$.ajax({
url: 'https://api.github.com/gists/' + self.gistID,
type: 'PATCH',
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'token ' + self.token);
},
data: JSON.stringify(data)
}).always(function(data, txtStatus, err) {
if (typeof callback === 'function') callback(data);
});
}
}

信用:http://techslides.com/github-gist-api-with-curl-and-ajax

关于javascript - 使用 ajax 编辑要点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52347382/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com