Support IE
This commit is contained in:
54
js/main.js
54
js/main.js
@ -1,53 +1,45 @@
|
|||||||
'use strict'
|
var backend = 'https://ntut.com.tw';
|
||||||
|
|
||||||
let backend = 'https://ntut.com.tw'
|
function shortenUrl() {
|
||||||
|
var shortCode = $('#urlBox .short-code').val() || null;
|
||||||
const shortenUrl = async () => {
|
var url = $('#urlBox .target-url').val();
|
||||||
let shortCode = $('#urlBox .short-code')[0].value || null
|
|
||||||
let url = $('#urlBox .target-url')[0].value
|
|
||||||
|
|
||||||
if (!url) {
|
if (!url) {
|
||||||
alert("URL is empty!")
|
alert("網址不能是空白的!");
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
request({ shortCode, url })
|
request(shortCode, url)
|
||||||
.done((res) => {
|
.done(function (res) {
|
||||||
console.log(res)
|
console.log(res);
|
||||||
|
addNewRow(res);
|
||||||
addNewRow(res)
|
|
||||||
})
|
})
|
||||||
.fail((err) => {
|
.fail(function (err) {
|
||||||
console.log(err)
|
console.log(err);
|
||||||
|
|
||||||
if (err.status == 409) {
|
if (err.status == 409) {
|
||||||
alert("This code has been used.")
|
alert("這個別名已經被使用了");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
alert("Failed to establish connection.")
|
alert("無法建立連線,請稍後再試")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const addNewRow = (data) => {
|
function addNewRow(data) {
|
||||||
let view = $('#urlBox .urls')
|
var rowHtml = '<div class="url row">';
|
||||||
|
rowHtml += '<p class="text-center col-md-4 col-6">ntut.com.tw/' + data.shortCode + '</p>';
|
||||||
|
rowHtml += '<p class="text-center col-md-8 col-6">' + data.url + '</p>';
|
||||||
|
rowHtml += '</div>';
|
||||||
|
|
||||||
view.append($(`
|
$('#urlBox .urls').append($(rowHtml));
|
||||||
<div class="url row">
|
|
||||||
<p class="text-center col-md-4 col-6">ntut.com.tw/${data.shortCode}</p>
|
|
||||||
<p class="text-center col-md-8 col-6">${data.url}</p>
|
|
||||||
</div>
|
|
||||||
`))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = ({ shortCode, url }) => {
|
function request(shortCode, url) {
|
||||||
return $.ajax({
|
return $.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
url: `${backend}/api/shortUrl`,
|
url: backend + '/api/shortUrl',
|
||||||
data: JSON.stringify({
|
data: JSON.stringify({ shortCode: shortCode, url: url }),
|
||||||
shortCode, url
|
|
||||||
}),
|
|
||||||
processData: false,
|
processData: false,
|
||||||
contentType: 'application/json; charset=UTF-8'
|
contentType: 'application/json; charset=UTF-8'
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user