NOP*_*ALL 2 javascript ajax firefox xmlhttprequest firefox-addon
可以在此处找到有关 API 的信息。它没有提供与 Javascript 一起使用的任何详细信息,只提供 curl 。
在这里的旧帖子中尝试了许多不同的方法,但这是我迄今为止最接近的方法。
function main() {
var ul = document.querySelector('.redactor_toolbar')
if(ul != null)
{
var new_li = document.createElement('li')
var new_a = document.createElement('a')
new_li.appendChild(new_a)
ul.appendChild(new_li)
new_a.addEventListener('click', function() {
var input = document.createElement('input');
input.type = 'file';
input.onchange = e => {
uploadImage(e.target.files[0])
}
input.click();
})
}
}
async function uploadImage(img)
{
var form = new FormData();
form.append('image', img)
var url = 'https://api.imgbb.com/1/upload?key=8d5867a9512390fb5e5dc97839aa36f6'
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
},
body: form
}
const response = await fetch(url, config)
const json = await response.json()
console.log(response)
}
Run Code Online (Sandbox Code Playgroud)
JSON 响应:
小智 6
对于 mi 应用程序也是同样的问题。
创建
<input type="file" id="input_img" onchange="fileChange()" accept="image/*">
Run Code Online (Sandbox Code Playgroud)
代码 javascript
function fileChange(){
var file = document.getElementById('input_img');
var form = new FormData();
form.append("image", file.files[0])
var settings = {
"url": "https://api.imgbb.com/1/upload?key=8d5867a9512390fb5e5dc97839aa36f6",
"method": "POST",
"timeout": 0,
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};
$.ajax(settings).done(function (response) {
console.log(response);
var jx = JSON.parse(response);
console.log(jx.data.url);
});
Run Code Online (Sandbox Code Playgroud)
}
这对我有用
| 归档时间: |
|
| 查看次数: |
4105 次 |
| 最近记录: |